UpdateSourceTrigger在IsChecked上的PropertyChanged不会在复选框的ListBox的ItemsSource上启动 [英] UpdateSourceTrigger PropertyChanged on IsChecked Isn't Firing on ItemsSource of ListBox of Checkboxes

查看:211
本文介绍了UpdateSourceTrigger在IsChecked上的PropertyChanged不会在复选框的ListBox的ItemsSource上启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这里是我的xaml ListBox

 < ListBox x:Name =lstRoleItemsSource ={Binding Path = FAccountFunctions,Mode = TwoWay ,UpdateSourceTrigger = PropertyChanged}SelectedValuePath =Id> 
< ListBox.ItemTemplate>
< DataTemplate>
< StackPanel Orientation =Horizo​​ntal>
< 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以使其工作。

 公共财产FAccountFunctions作为列表(FunctionType)
获取
返回_faccountFunctions
结束Get
Set(ByVal value As List(Of FunctionType))
_faccountFunctions = value
Filter()
结束集
结束属性

在FAccountFunctions集合的SelectedFlag属性上引发PropertyChangedEvent。其中一个属性SelectedFlag更改时,如何在项目源上引发事件?



将FAccountFunctions属性更改为ObservableCollection ...没有运气。

解决方案

当您的项目的PropertyChanged事件触发时,您将需要使您的Collection的CollectionChanged事件触发。



如下所示:

  MyCollection.CollectionChanged + = MyCollectionChanged; 

...

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

if(e.OldItems!= null)
{
foreach(e.OldItems中的对象项)
{
if(item is MyItem)
((MyItem)item).PropertyChanged - = MyItem_PropertyChanged;
}
}
}

...

  void MyItem_PropertyChanged(object sender,PropertyChangedEventArgs e)
{
OnPropertyChanged(MyCollection);
}


I have a listbox in which the item source contains a List(of T) that has a SelectedFlag boolean property. My viewmodel is set as the DataContext of my user control and everything is working as expected except I can't get the property change even when a check box is changed.

Here is my 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>

I need to call my Filter() function after a check box is checked and I normally would set the UpdateSourcTrigger=PropertyChanged to make this work.

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

The PropertyChangedEvent is getting raised on the 'SelectedFlag' property in the FAccountFunctions collection. How can I raise an event on the items source when one of the properties SelectedFlag changes?

Changed my FAccountFunctions property to an ObservableCollection...no luck.

解决方案

You'll need to make your Collection's CollectionChanged event fire when your Item's PropertyChanged event fires.

Something like:

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");
}

这篇关于UpdateSourceTrigger在IsChecked上的PropertyChanged不会在复选框的ListBox的ItemsSource上启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆