2

アイテムソースにSelectedFlagブールプロパティを持つList(of T)が含まれているリストボックスがあります。ビューモデルはユーザーコントロールのDataContextとして設定されており、チェックボックスを変更してもプロパティの変更を取得できないことを除いて、すべてが期待どおりに機能しています。

これが私のxamlリストボックスです

<ListBox x:Name="lstRole" ItemsSource="{Binding Path=FAccountFunctions, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Id">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding Path=SelectedFlag, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" />
                            <TextBlock Text="{Binding Path=FunctionDesc}" VerticalAlignment="Center" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

チェックボックスがオンになったらFilter()関数を呼び出す必要があり、通常はUpdateSourcTrigger=PropertyChangedを設定してこれを機能させます。

Public Property FAccountFunctions As List(Of FunctionType)
        Get
            Return _faccountFunctions
        End Get
        Set(ByVal value As List(Of FunctionType))
            _faccountFunctions = value
            Filter()
        End Set
    End Property

PropertyChangedEventは、FAccountFunctionsコレクションの'SelectedFlag'プロパティで発生します。プロパティSelectedFlagの1つが変更されたときに、アイテムソースでイベントを発生させるにはどうすればよいですか?

FAccountFunctionsプロパティをObservableCollectionに変更しました...運がありません。

4

1 に答える 1

3

アイテムのPropertyChangedイベントが発生したときに、コレクションのCollectionChangedイベントを発生させる必要があります。

何かのようなもの:

MyCollection.CollectionChanged += MyCollectionChanged;

..。

void MyCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    if (e.NewItems != null)
    {
        foreach (object item in e.NewItems)
        {
            if (item is MyItem)
                ((MyItem)item).PropertyChanged += MyItem_PropertyChanged;
        }
    }

    if (e.OldItems != null)
    {
        foreach (object item in e.OldItems)
        {
            if (item is MyItem)
                ((MyItem)item).PropertyChanged -= MyItem_PropertyChanged;
        }
    }
}

..。

void MyItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    OnPropertyChanged("MyCollection");
}
于 2011-04-18T20:07:06.033 に答える