3

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 を初めて使用するので、少し迷っています。

編集

  1. クラスTreeViewModelは を実装しINotifyPropertyChangedます。
  2. ConditionsIsCheckedから値を取得することによって更新されTreeViewます。
  3. ステータス リストのサイズは変更されません。TreeViewItem が選択/選択解除されると、TreeViewModel が変更されます。
  4. TreeViewModel ソース (このページの FooViewModel )
  5. 上記のバインディング コード。
  6. のバインド モードを変更する必要はありませんでした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>
    
4

1 に答える 1

8

リスト参照自体が変更された場合ではなく、リストに追加/削除/変更されupdateConditionsたときにいつでも起動したいと思います。item

ObservableCollection<T>TreeViewModel 内に INotifyPropertyChanged を実装しているので、プレーンの代わりに使用したいと思いますList<T>。ここで確認してください: http://msdn.microsoft.com/en-us/library/ms668604.aspx

項目が追加、削除されたとき、またはリスト全体が更新されたときに通知を提供する動的データ コレクションを表します。

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 は、アイテムが追加/削除/変更されるたびに発生します。私の知る限り、参照が変更されたとき、またはそのプロパティのいずれかが変更されたときに「変更された」と見なされます(これは を介し​​て通知されますINotifyPropertyChanged

ここで確認しました:http://msdn.microsoft.com/en-us/library/ms653375.aspx

ObservableCollection.CollectionChanged イベント アイテムが追加、削除、変更、移動されたとき、またはリスト全体が更新されたときに発生します。

ObservableCollection<T>アセンブリ内のSystem.Collections.ObjectModel名前空間に存在します。System.dll

于 2013-04-11T14:25:24.640 に答える