Wpf Combobox itemssource 绑定回调
WpfCombo box itemsource binding call back
我有一个组合框,它绑定到视图模型列表 属性。此列表 属性 然后调用数据层中的异步函数。
我想将组合框的选定索引 属性 设置为 "zero" 即选定索引=0;
实际情况是数据正在完美加载,但即使在我设置了选定索引后 属性 自异步调用以来它也没有应用。
属性绑定后有什么回调方法请告诉我
我做了一个示例应用程序
视图模型
class MainWindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<string> _source;
private object _lock = new object();
public IEnumerable<string> Source
{
get { return _source; }
}
public String SelectedItem { get; set; }
public int SelectedIndex { get; set; }
public MainWindowViewModel()
{
_source = new ObservableCollection<string>();
BindingOperations.EnableCollectionSynchronization(_source, _lock);
Task.Factory.StartNew(() => PopulateSourceAsunc());
}
private void PopulateSourceAsunc()
{
for (int i = 0; i < 10; i++)
{
_source.Add("Test " + i.ToString());
Thread.Sleep(1000);
}
//SelectedItem = _source[6];
SelectedIndex = 5;
OnPropertyChanged("SelectedIndex");
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Xaml
<StackPanel>
<ComboBox ItemsSource="{Binding Source}"
SelectedItem="{Binding SelectedItem}"
SelectedIndex="{Binding SelectedIndex}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</StackPanel>
代码隐藏
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
BindingOperations
class 在 System.Windows.Data
的命名空间中
我有一个组合框,它绑定到视图模型列表 属性。此列表 属性 然后调用数据层中的异步函数。
我想将组合框的选定索引 属性 设置为 "zero" 即选定索引=0;
实际情况是数据正在完美加载,但即使在我设置了选定索引后 属性 自异步调用以来它也没有应用。
属性绑定后有什么回调方法请告诉我
我做了一个示例应用程序
视图模型
class MainWindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<string> _source;
private object _lock = new object();
public IEnumerable<string> Source
{
get { return _source; }
}
public String SelectedItem { get; set; }
public int SelectedIndex { get; set; }
public MainWindowViewModel()
{
_source = new ObservableCollection<string>();
BindingOperations.EnableCollectionSynchronization(_source, _lock);
Task.Factory.StartNew(() => PopulateSourceAsunc());
}
private void PopulateSourceAsunc()
{
for (int i = 0; i < 10; i++)
{
_source.Add("Test " + i.ToString());
Thread.Sleep(1000);
}
//SelectedItem = _source[6];
SelectedIndex = 5;
OnPropertyChanged("SelectedIndex");
}
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Xaml
<StackPanel>
<ComboBox ItemsSource="{Binding Source}"
SelectedItem="{Binding SelectedItem}"
SelectedIndex="{Binding SelectedIndex}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</StackPanel>
代码隐藏
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
BindingOperations
class 在 System.Windows.Data