如何从子视图模型绑定到父视图模型中的事件

时间:2015-04-13 23:42:58

标签: c# wpf xaml mvvm treeview

我目前正在关注设置带复选框的TreeView的this指南。在我的代码中,树" FooViewModel"在我的MainViewModel中启动,并作为ItemsSource绑定到TreeView。我希望能够订阅MainViewModel中的某些事件,该事件将在选中或取消选中某些内容时触发。这样我就可以遍历" FooViewModel"并检查哪些节点有IsChecked = True。如何创建此事件绑定?

这是我的代码:

<Style x:Key="TreeViewItemStyle" TargetType="TreeViewItem">
    <Setter Property="IsExpanded" Value="False" />
    <Setter Property="IsSelected" Value="{Binding IsInitiallySelected, Mode=OneTime}" />
    <Setter Property="KeyboardNavigation.AcceptsReturn" Value="True" />
    <Setter Property="xn:VirtualToggleButton.IsVirtualToggleButton" Value="True" />
    <Setter Property="xn:VirtualToggleButton.IsChecked" Value="{Binding IsChecked}" />
    <Setter Property="Focusable" Value="False" />
</Style>

<xn:TreeView ItemsSource="{Binding CollectionFooViewModel}" ItemContainerStyle="{StaticResource TreeViewItemStyle}">
    <xn:TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children, Mode=OneTime}">
            <StackPanel Orientation="Horizontal">
                <CheckBox Focusable="False" IsChecked="{Binding IsChecked}" VerticalAlignment="Center"/>
                <ContentPresenter Content="{Binding Name, Mode=OneTime}" Margin="2,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>
    </xn:TreeView.ItemTemplate>
</xn:TreeView>

我想是否有办法绑定&#34; IsChecked&#34;两个属性(一个在FooViewModel中,另一个在MainViewModel中)我会得到答案。

2 个答案:

答案 0 :(得分:1)

实现这一目标的方法很多。一种是某种发布/发布(消息)实现,或者只是一堆Action代表?有点像...

<强>主窗口

<Window x:Class="WpfApplication1.View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow"
        Height="300"
        Width="250">

    <TreeView ItemsSource="{Binding CollectionFooViewModel}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children, Mode=OneTime}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox Focusable="False" 
                              IsChecked="{Binding IsChecked}"
                              VerticalAlignment="Center"/>
                    <ContentPresenter Content="{Binding Name, Mode=OneTime}"
                                      Margin="2,0"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</Window>

<强>的DataContext

public class MainViewModel : ViewModelBase
{
    public MainViewModel()
    {
        Action<MyItem> action = item => Console.WriteLine(@"MyItem was clicked");

        CollectionFooViewModel = new ObservableCollection<MyItem>()
        {
            new MyItem()
            {
                Name = "MyItem1",
                Children = new List<MyItem>()
                { 
                    new MyItem()
                    {
                        Name = "MySubItem1", 
                        IsChecked = false, 
                        Action = item => Console.WriteLine(@"{0} invoked action", item.Name)
                    },
                    new MyItem()
                    {
                        Name = "MySubItem2", 
                        IsChecked = true, 
                        Action = item => Console.WriteLine(@"{0} state is {1} ", item.Name, item.IsChecked)
                    },
                },
                Action = action
            }
        };
    }

    public ObservableCollection<MyItem> CollectionFooViewModel { get; set; }
}

public class MyItem : ViewModelBase
{
    private bool _isChecked;
    public string Name { get; set; }
    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;

            if (Action != null)
                Action.BeginInvoke(this, null, null);
        }
    }

    public IEnumerable<MyItem> Children { get; set; }
    public Action<MyItem> Action { get; set; }
}

它为您提供以下内容......

imgur

...按顺序点击时将其吐出到控制台。

  

单击了MyItem   MySubItem1调用了动作
  MySubItem2状态为False

当然,在您的情况下,您可能希望将具体方法传递给委托。

答案 1 :(得分:0)

尝试添加&#34; OnPropertyEventChanged&#34;方法调用你的模型的setter,这是我的意思的一个例子:

https://msdn.microsoft.com/en-us/library/ms743695%28v=vs.110%29.aspx