wpf 属性改变时的WPF调用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15951173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:33:46  来源:igfitidea点击:

WPF call method when property changes

c#wpfproperties

提问by mauryat

In C#, how can a method be called when a property changes (both method and property belong to the same class)?

在C#中,如何在属性改变时调用方法(方法和属性都属于同一个类)?

e.g.,

例如,

class BrowserViewModel
{
    #region Properties

    public List<TreeViewModel> Status { get; private set; }
    public string Conditions { get; private set; }

    #endregion // Properties

    // i'd like to call this method when Status gets updated
    void updateConditions
    {
         /* Conditions = something depending on the TreeViewItem select status */
    }
}

Binding

捆绑

<TreeView Grid.Row="1"
    x:Name="StatusTree"
    ItemContainerStyle="{StaticResource TreeViewItemStyle}"
    ItemsSource="{Binding Path=Status, Mode=OneTime}"
    ItemTemplate="{StaticResource CheckBoxItemTemplate}"
/>

Use-Case(if you are curious)

用例(如果你好奇的话)

The property Statusis bound to a TreeViewcontrol in the xaml. When it is updated, I'd like to call a method that updates the property Conditions. This property is bound to a TextBoxin the xaml.

该属性Status绑定到TreeViewxaml 中的控件。当它更新时,我想调用一个更新属性的方法Conditions。此属性绑定到TextBoxxaml 中的 a。

I'm new to Eventing in C#, so am a little lost.

我是 C# 中的 Eventing 新手,所以有点迷茫。

Edit

编辑

  1. class TreeViewModelimplements INotifyPropertyChanged.
  2. Conditionsis updated by getting the IsCheckedValue from the TreeView.
  3. The size of the Status List never changes. When a TreeViewItem is selected/unselected the TreeViewModel changes.
  4. TreeViewModel source (FooViewModel on thispage)
  5. Binding code above.
  6. Didn't have to change Binding Mode for IsChecked.

        <HierarchicalDataTemplate 
            x:Key="CheckBoxItemTemplate"
            ItemsSource="{Binding Children, Mode=OneTime}"
            >
                <StackPanel Orientation="Horizontal">
                <!-- These elements are bound to a TreeViewModel object. -->
                <CheckBox
                    Focusable="False" 
                    IsChecked="{Binding IsChecked}" 
                    VerticalAlignment="Center"
                    />
                <ContentPresenter 
                    Content="{Binding Name, Mode=OneTime}" 
                    Margin="2,0"
                    />
                </StackPanel>
        </HierarchicalDataTemplate>
    
  1. TreeViewModel实现INotifyPropertyChanged.
  2. Conditions通过IsCheckedTreeView.
  3. 状态列表的大小永远不会改变。When a TreeViewItem is selected/unselected the TreeViewModel changes.
  4. TreeViewModel源(FooViewModel在页)
  5. 上面的绑定代码。
  6. 不必更改 .bin 的绑定模式IsChecked

        <HierarchicalDataTemplate 
            x:Key="CheckBoxItemTemplate"
            ItemsSource="{Binding Children, Mode=OneTime}"
            >
                <StackPanel Orientation="Horizontal">
                <!-- These elements are bound to a TreeViewModel object. -->
                <CheckBox
                    Focusable="False" 
                    IsChecked="{Binding IsChecked}" 
                    VerticalAlignment="Center"
                    />
                <ContentPresenter 
                    Content="{Binding Name, Mode=OneTime}" 
                    Margin="2,0"
                    />
                </StackPanel>
        </HierarchicalDataTemplate>
    

回答by Conrad Clark

I assume you want updateConditionsto fire whenever an itemis added/removed/changed in your list, not if the list reference itself changes.

我假设您想在列表中添加/删除/更改updateConditionsan 时触发item,而不是在列表引用本身发生更改时触发。

Since you're implementing INotifyPropertyChanged within your TreeViewModel, I think you'll want to use ObservableCollection<T>instead of a plain List<T>. Check it here: http://msdn.microsoft.com/en-us/library/ms668604.aspx

由于您在 TreeViewModel 中实现 INotifyPropertyChanged,我认为您会想要使用ObservableCollection<T>而不是普通的List<T>. 在这里检查:http: //msdn.microsoft.com/en-us/library/ms668604.aspx

Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

表示动态数据集合,在添加、删除项目或刷新整个列表时提供通知。

class BrowserViewModel
{
    #region Properties

    public ObservableCollection<TreeViewModel> Status { get; private set; }
    public string Conditions { get; private set; }

    #endregion // Properties

    // i'd like to call this method when Status gets updated
    void updateConditions
    {
         /* Conditions = something */
    }

    public BrowserViewModel()
    {
        Status = new ObservableCollection<TreeViewModel>();
        Status.CollectionChanged += (e, v) => updateConditions();
    }
}

CollectionChanged will fire whenever an item is added/removed/changed. As far as I know, it will consider it "changed" when its reference changes or any of its properties are changed (which is notified through INotifyPropertyChanged)

每当添加/删除/更改项目时,CollectionChanged 都会触发。据我所知,当它的引用更改或其任何属性发生更改(通过 通知INotifyPropertyChanged)时,它会认为它“已更改”

Just checked it here: http://msdn.microsoft.com/en-us/library/ms653375.aspx

刚刚在这里检查:http: //msdn.microsoft.com/en-us/library/ms653375.aspx

ObservableCollection.CollectionChanged Event Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.

ObservableCollection.CollectionChanged 事件在添加、删除、更改、移动项目或刷新整个列表时发生。

ObservableCollection<T>resides in the System.Collections.ObjectModelnamespace, in System.dllassembly.

ObservableCollection<T>驻留在System.Collections.ObjectModel命名空间中,在System.dll程序集中。