简体   繁体   English

属性更改时的WPF调用方法

[英]WPF call method when property changes

In C#, how can a method be called when a property changes (both method and property belong to the same class)? 在C#中,如何在属性更改时调用方法(方法和属性都属于同一个类)?

eg, 例如,

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 Status is bound to a TreeView control in the xaml. 属性Status绑定到xaml中的TreeView控件。 When it is updated, I'd like to call a method that updates the property Conditions . 更新后,我想调用一个更新属性Conditions This property is bound to a TextBox in the xaml. 此属性绑定到xaml中的TextBox

I'm new to Eventing in C#, so am a little lost. 我是C#中Eventing的新手,所以有点迷失。

Edit 编辑

  1. class TreeViewModel implements INotifyPropertyChanged . class TreeViewModel实现了INotifyPropertyChanged
  2. Conditions is updated by getting the IsChecked Value from the TreeView . 通过从TreeView获取IsChecked值来更新Conditions
  3. The size of the Status List never changes. 状态列表的大小永远不会改变。 When a TreeViewItem is selected/unselected the TreeViewModel changes. 选择/取消选择TreeViewItem时,TreeViewModel会更改。
  4. TreeViewModel source (FooViewModel on this page) TreeViewModel源代码( 页面上的FooViewModel)
  5. Binding code above. 上面的绑定代码。
  6. Didn't have to change Binding Mode for IsChecked . 没有必要更改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> 

I assume you want updateConditions to fire whenever an item is added/removed/changed in your list, not if the list reference itself changes. 我假设您希望updateConditions在列表中添加/删除/更改item时触发,而不是列表引用本身更改时触发。

Since you're implementing INotifyPropertyChanged within your TreeViewModel, I think you'll want to use ObservableCollection<T> instead of a plain List<T> . 由于您在TreeViewModel中实现了INotifyPropertyChanged,我认为您将要使用ObservableCollection<T>而不是普通的List<T> Check it here: http://msdn.microsoft.com/en-us/library/ms668604.aspx 请在此处查看: 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. 每当添加/删除/更改项目时,CollectionChanged都将触发。 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 ) 据我所知,当引用更改或其任何属性发生更改时(通过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.ObjectModel namespace, in System.dll assembly. ObservableCollection<T>驻留在System.dll程序集中的System.Collections.ObjectModel命名空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM