我有一个ComboBox,其ItemsSource绑定到链接到ObservableCollection的新(非默认)ListCollectionView。 ComboBox SelectedItem属性绑定到公共SelectedHat属性。ComboBox的SelectedItem在实际值之前意外地设置为空
步骤1:在ComboBox中选择第二项。按照预期,SelectedHat现在是列表中的第二个帽子。 步骤2 :(点击按钮)将列表中的第二个点设置为新的帽子。 SelectedHat首先设置为null,然后设置为新的Hat。
为什么在新帽子之前将SelectedHat设置为null?
欲能够vm.Collection [索引] =新帽()和
(1)如果ComboBox该索引选择已,保持选定的去空白
(2)只设置SelectedHat代替一旦新的帽子,而不是空,然后将新帽
C#:
public partial class MainWindow : Window
{
private readonly ViewModel vm;
public MainWindow()
{
InitializeComponent();
vm = new ViewModel();
DataContext = vm;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Hat item = new Hat { Name = "hat 2", Color = "Red"};
vm.Collection[1] = item;
}
}
public class ViewModel : BaseNotifyPropertyChanged
{
public ObservableCollection<Hat> Collection { get; set; }
public ListCollectionView View { get; set; }
private Hat selectedHat;
public Hat SelectedHat
{
get { return selectedHat; }
set
{
selectedHat = value;
Console.WriteLine(string.Format("SelectedHat set to [{0}]", value));
NotifyPropertyChanged("SelectedHat");
}
}
public ViewModel()
{
Collection = new ObservableCollection<Hat>()
{
new Hat { Name = "hat 1", Color = "Black" },
new Hat { Name = "hat 2", Color = "Black" },
new Hat { Name = "hat 3", Color = "Black" },
};
View = new ListCollectionView(Collection);
View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
}
}
public class Hat
{
public string Name { get; set; }
public string Color { get; set; }
public override string ToString()
{
return string.Format("{0} ({1})", Name, Color);
}
}
public abstract class BaseNotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML:
<StackPanel>
<TextBlock Text="{Binding Path=SelectedHat, Mode=OneWay}" />
<ComboBox ItemsSource="{Binding Path=View}" SelectedItem="{Binding Path=SelectedHat, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="click me" Click="Button_Click" />
</StackPanel>
查看我的回答。直接更新该项目并且没有调用OnCollectionChanged。 – Paparazzi 2012-03-22 22:46:38
是的,我想说的是除了你提出的建议之外的其他工作:-) – Phil 2012-03-23 07:26:58