IsChecked上的UpdateSourceTrigger PropertyChanged没有在Checkbox的ListBox的ItemsSource上触发

时间:2011-04-18 17:46:34

标签: wpf data-binding checkedlistbox

我有一个列表框,其中项源包含一个具有SelectedFlag布尔属性的List(of T)。我的viewmodel被设置为我的用户控件的DataContext,一切都按预期工作,除非我更改属性,即使更改了复选框。

这是我的xaml ListBox

<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 ...没有运气。

1 个答案:

答案 0 :(得分: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");
}
相关问题