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

在 FAccountFunctions 集合中的“SelectedFlag”属性上引发了 PropertyChangedEvent。当 SelectedFlag 属性之一发生更改时,如何在项目源上引发事件?

将我的 FAccountFunctions 属性更改为 ObservableCollection ......不走运。

4

1 回答 1

3

当您的 Item 的 PropertyChanged 事件触发时,您需要使您的 Collection 的 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 回答