简体   繁体   English

如何将网格绑定到WPF中的选定树视图项目

[英]How to bind grid to selected tree view item in WPF

How do I bind grid to selected tree view item in WPF? 如何将网格绑定到WPF中的选定树视图项目? In short when user of my app clicks on tree view item, then I want to run command that would load some data and present it on grid. 简而言之,当我的应用程序用户单击树视图项目时,我想运行将加载一些数据并将其显示在网格上的命令。 Based on item's underlying type I want to bind to different commands. 基于项目的基础类型,我想绑定到不同的命令。

XAML XAML

<TreeView ItemsSource="{Binding Path=SomeCollection, UpdateSourceTrigger=PropertyChanged}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=., Converter={StaticResource GetElementTypeConverter}}" Value="{x:Type Models:SomeType}">
                    // WHAT CODE GOES HERE?
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=., Converter={StaticResource GetElementTypeConverter}}" Value="{x:Type Models:SomeOtherType}">
                    // WHAT CODE GOES HERE?
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

I'd like to handle click event in MVVM manner. 我想以MVVM方式处理click事件。

In WPF, we deal with different data types by defining different DataTemplate s. 在WPF中,我们通过定义不同的DataTemplate处理不同的数据类型。 In your case, you could already have defined some. 就您而言,您可能已经定义了一些。 If not, do so now, and in these DataTemplate s, you can set the relevant ICommand s. 如果不是,请立即执行,并在这些DataTemplate ,可以设置相关的ICommand You can do this in several ways, but one easy way is just to define your content inside a 'blank' Button : 您可以通过多种方式来执行此操作,但是一种简单的方法是仅在“空白” Button定义内容:

<DataTemplate DataType="{x:Type YourDataTypePrefix:YourDataType}">
    <Button Command="{Binding SomeCommand}">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <!-- Define your YourDataType item content here -->
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
</DataTemplate>
<DataTemplate DataType="{x:Type YourDataTypePrefix:OtherDataType}">
    <Button Command="{Binding SomeOtherCommand}">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <!-- Define your OtherDataType item content here -->
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
</DataTemplate>

UPDATE >>> 更新>>>

That seems strange that you say you can't select the TreeViewItem with that Button . 您说无法使用那个Button选择TreeViewItem似乎很奇怪。 However, you could try using a ToggleButton to bind to the TreeViewItem.IsSelected property instead: 但是,您可以尝试使用ToggleButton绑定到TreeViewItem.IsSelected属性:

<ToggleButton Command="{Binding SomeCommand}" IsChecked="{Binding IsSelected, 
    RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}">
    <ToggleButton.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <!-- Define your YourDataType item content here -->
            </Grid>
        </ControlTemplate>
    </ToggleButton.Template>
</ToggleButton>

If that still doesn't work, then use an AttachedProperty to handle the PreviewMouseDown event on a Grid in the DataTemplate s. 如果仍然不能解决问题,请使用AttachedProperty处理DataTemplate Grid上的PreviewMouseDown事件。 You can find lots of online documentation about handling events with AttachedProperty s, so I won't go over that again here. 您可以找到许多有关使用AttachedProperty处理事件的在线文档,因此在此不再赘述。 However, if you did that (and that is the better solution), then you could then do something like this: 但是,如果您这样做了(那是更好的解决方案),那么您可以执行以下操作:

<DataTemplate DataType="{x:Type YourDataTypePrefix:YourDataType}">
    <Grid Attached:MouseEvents.PreviewMouseDown="{Binding SomeCommand}">
        <!-- Define your YourDataType item content here -->
    </Grid>
</DataTemplate>

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

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