ObservableCollection にリンクされている新しい (デフォルトではない) ListCollectionView に ItemsSource がバインドされている ComboBox があります。ComboBox の SelectedItem プロパティは、公開されている SelectedHat プロパティにバインドされています。
ステップ 1: ComboBox で 2 番目の項目を選択します。SelectedHat は、予想どおり、リストの 2 番目のハットになりました。ステップ 2: (ボタンをクリックして) リストの 2 番目のスポットを新しいハットに設定します。SelectedHat は最初に null に設定され、次に新しい Hat に設定されます。
新しいハットの前に SelectedHat が null に設定されるのはなぜですか?
vm.Collection[index] = new Hat() を実行できるようにしたい
(1) ComboBox でそのインデックスが選択されている場合は、空白にするのではなく選択したままにする
(2) SelectedHat を新しいハットに一度だけ設定する代わりにnull および THEN 新しいハット
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>