我遇到了与ComboBox
有关的问题,该问题绑定到ObservableCollection
,我想知道是否有人能指出我缺少的内容。ComboBox SelectedItem在清除可观察集合后不会更改
我有一个ComboBox
这是绑定到一个简单的ObservableCollection<string>
。另外,我还将SelectedIndex
绑定到某个属性的OneWay
。
在我的应用程序中,我得到了一个要清除收集并重新填充不同数据并将SelectedIndex
设置为新值的点。由于某种原因,SelectedIndex
绑定不起作用。
我安装的问题一点点摄制:
public partial class Window1 : Window, INotifyPropertyChanged
{
private int j;
public event PropertyChangedEventHandler PropertyChanged;
public Window1()
{
InitializeComponent();
DataContext = this;
Tables = new ObservableCollection<string>();
}
public ObservableCollection<string> Tables { get; set; }
private int _TheIndex;
public int TheIndex
{
get { return _TheIndex; }
set
{
_TheIndex = value;
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("TheIndex"));
}
}
}
private void aaaa(object sender, RoutedEventArgs e)
{
j = (j + 1)%10;
Tables.Clear();
for(int i = 0; i < 10 ; i++)
{
Tables.Add(i.ToString());
}
TheIndex = j;
}
}
的XAML是:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<StackPanel>
<ComboBox x:Name="TablesCombobox"
ItemsSource="{Binding Tables}"
SelectedIndex="{Binding TheIndex, Mode=OneWay}"/>
<Button Content="asdasd" Click="aaaa"/>
</StackPanel>
</Grid>
</Window>
为什么你不只是在aaaa()中设置TablesCombobox.SelectedIndex而不是TheIndex?不知道为什么绑定不起作用。 – 2010-01-15 21:03:41
我只需将您的repro代码复制/粘贴到VS2010 Express Beta 2中即可使用 - 并且工作顺利... – 2010-01-15 21:57:50