0

機能を実現するための適切なイベントが見つかりませんでした。

TargetUpdated イベントが機能しませんでした。

xaml で SelectedIndex を 0 に設定すると、データの最初のロードにのみ影響します。

4

3 に答える 3

4

あなたはできる:

  • NotifyOnTargetUpdatedバインディングにセット
  • のイベント ハンドラーを追加します。Binding.TargetUpdated
  • そのイベントハンドラーで登録するItemsSource.CollectionChanged
  • そのイベント ハンドラで、選択したインデックスをゼロに設定します

この問題はNotifyonTargetUpdated、バインディングで設定しなかったために最初のイベントが発生しなかったか、コレクションが更新されていたが同じコレクションだったために 2 番目のイベントが必要になった可能性があります。

イベントが発生したときに実行したいことを実行するための aおよびa プロキシListBoxとしてaを使用する実際の例を次に示します。ItemsControlMessageBox

マークアップは次のとおりです。

<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!");
}
于 2011-02-23T04:55:04.837 に答える
0

私は同じ問題に直面しました。この問題を克服するために、次の手順を使用しました。

  • テキストボックスを作成する
  • TextBox の可視性を Collapsed に設定する
  • テキストをバインド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;
    
    }
    
于 2012-09-17T05:37:38.277 に答える
0

SourceUpdatedイベントを試しましたか?

于 2011-02-23T04:47:14.940 に答える