機能を実現するための適切なイベントが見つかりませんでした。
TargetUpdated イベントが機能しませんでした。
xaml で SelectedIndex を 0 に設定すると、データの最初のロードにのみ影響します。
機能を実現するための適切なイベントが見つかりませんでした。
TargetUpdated イベントが機能しませんでした。
xaml で SelectedIndex を 0 に設定すると、データの最初のロードにのみ影響します。
あなたはできる:
NotifyOnTargetUpdated
バインディングにセットBinding.TargetUpdated
ItemsSource.CollectionChanged
この問題はNotifyonTargetUpdated
、バインディングで設定しなかったために最初のイベントが発生しなかったか、コレクションが更新されていたが同じコレクションだったために 2 番目のイベントが必要になった可能性があります。
イベントが発生したときに実行したいことを実行するための aおよびa プロキシListBox
としてaを使用する実際の例を次に示します。ItemsControl
MessageBox
マークアップは次のとおりです。
<Grid>
<DockPanel>
<Button DockPanel.Dock="Top" Content="Update Target" Click="ButtonUpdateTarget_Click"/>
<Button DockPanel.Dock="Top" Content="Update Item" Click="ButtonUpdateItem_Click"/>
<ListBox Name="listBox" Binding.TargetUpdated="ListBox_TargetUpdated" ItemsSource="{Binding Items, NotifyOnTargetUpdated=True}"/>
</DockPanel>
</Grid>
コードビハインドは次のとおりです。
public class ViewModel : INotifyPropertyChanged
{
ObservableCollection<string> items;
public ObservableCollection<string> Items
{
get { return items; }
set { items = value; OnPropertyChanged("Items"); }
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
void SetDataContext()
{
DataContext = viewModel;
viewModel.Items = new ObservableCollection<string> { "abc", "def", "ghi" };
}
ViewModel viewModel = new ViewModel();
private void ButtonUpdateTarget_Click(object sender, RoutedEventArgs e)
{
viewModel.Items = new ObservableCollection<string> { "xyz", "pdq" };
}
private void ButtonUpdateItem_Click(object sender, RoutedEventArgs e)
{
viewModel.Items[0] = "xxx";
}
private void ListBox_TargetUpdated(object sender, DataTransferEventArgs e)
{
MessageBox.Show("Target Updated!");
(listBox.ItemsSource as INotifyCollectionChanged).CollectionChanged += new NotifyCollectionChangedEventHandler(listBox_CollectionChanged);
}
void listBox_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
MessageBox.Show("Item Updated!");
}
私は同じ問題に直面しました。この問題を克服するために、次の手順を使用しました。
テキストをバインドListBox.Items.Count
<TextBox x:Name="txtCount" TextChanged="TextBox_TextChanged" Text="{Binding ElementName=ListBox1, Path=Items.Count, Mode=OneWay}" Visibility="Collapsed" />
TextBox_TextChanged
イベント時は0SelectedIndex
に設定
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
int count = 0;
if(int.TryParse(txtCount.Text,out count) && count>0)
ListBox1.SelectedIndex = 0;
}
SourceUpdatedイベントを試しましたか?