[英]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的新手,所以有点迷失。
TreeViewModel
implements INotifyPropertyChanged
. TreeViewModel
实现了INotifyPropertyChanged
。 Conditions
is updated by getting the IsChecked
Value from the TreeView
. TreeView
获取IsChecked
值来更新Conditions
。 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.