C#では、プロパティが変更されたときにメソッドをどのように呼び出すことができますか(メソッドとプロパティの両方が同じクラスに属しています)?
例えば、
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 */
}
}
バインディング
<TreeView Grid.Row="1"
x:Name="StatusTree"
ItemContainerStyle="{StaticResource TreeViewItemStyle}"
ItemsSource="{Binding Path=Status, Mode=OneTime}"
ItemTemplate="{StaticResource CheckBoxItemTemplate}"
/>
ユースケース(興味がある場合)
プロパティは、xamlStatus
のコントロールにバインドされています。TreeView
更新されたら、プロパティを更新するメソッドを呼び出したいConditions
です。このプロパティはTextBox
、xaml の にバインドされています。
私は C# で Eventing を初めて使用するので、少し迷っています。
編集
- クラス
TreeViewModel
は を実装しINotifyPropertyChanged
ます。 Conditions
IsChecked
から値を取得することによって更新されTreeView
ます。- ステータス リストのサイズは変更されません。TreeViewItem が選択/選択解除されると、TreeViewModel が変更されます。
- TreeViewModel ソース (このページの FooViewModel )
- 上記のバインディング コード。
のバインド モードを変更する必要はありませんでした
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>