简体   繁体   English

在命令参数中发送当前项目和复选框值

[英]Send current Item and checkbox value in command parameters

I have a TreeView setup with a HierarchialDataTemplate .我有一个带有HierarchialDataTemplateTreeView设置。 It's ItemsSource is bound to a collection of Overlay objects in my viewmodel, where each Overlay has a collection of Layer objects (thus the HierarchialDataTemplate ).它的ItemsSource绑定到我的视图模型中的Overlay对象集合,其中每个Overlay都有一个Layer对象的集合(因此是HierarchialDataTemplate )。 For each Overlay , I'm displaying a CheckBox and a Label which is simply bound to the Overlay 's Name property.对于每个Overlay ,我将显示一个CheckBox和一个Label ,它们只是绑定到OverlayName属性。

What I'm trying to do is, each time one of the checkboxes is checked/unchecked, the current Overlay and the IsChecked property of the CheckBox will be sent as command parameters to my viewmodel.我想要做的是,每次选中/取消选中其中一个复选框时, CheckBox的当前OverlayIsChecked属性将作为命令参数发送到我的视图模型。

If I'm not using the MultiValueConverter , I can send one of the properties fine.如果我不使用MultiValueConverter ,我可以很好地发送其中一个属性。 But I need to send both as parameters.但我需要将两者都作为参数发送。

Below is the related.xaml for the treeview.下面是treeview的相关.xaml。 I'm only showing the necessary parts and just the Checked trigger because the Unchecked is exactly the same:我只显示必要的部分和Checked触发器,因为Unchecked完全相同:

<TreeView ItemsSource="{Binding OverlaysViewSource}" Name="LayersTreeView">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Layers}" >
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="True">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Checked">
                                <i:InvokeCommandAction Command="{Binding DataContext.SetVisibilityCmd, RelativeSource={RelativeSource AncestorType=UserControl}}" >
                                    <i:InvokeCommandAction.CommandParameter>
                                        <MultiBinding Converter="{StaticResource multiValueConverter}">
                                            <Binding Path="IsChecked, RelativeSource={RelativeSource AncestorType=CheckBox}" />
                                            <Binding/>
                                        </MultiBinding>
                                    </i:InvokeCommandAction.CommandParameter>
                                </i:InvokeCommandAction>
                            </i:EventTrigger>
                    </i:Interaction.Triggers>
                </CheckBox>
                <Label Content="{Binding Name}" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

So in the MultiBinding , the first one: <Binding Path="IsChecked, RelativeSource={RelativeSource AncestorType=CheckBox}" /> to try and send the checkbox's IsChecked property.所以在MultiBinding中,第一个: <Binding Path="IsChecked, RelativeSource={RelativeSource AncestorType=CheckBox}" />尝试发送复选框的IsChecked属性。 However, the value I'm getting in the command is DependencyProperty.UnsetValue .但是,我在命令中得到的值是DependencyProperty.UnsetValue

The second one is just for the current Overlay item, but the whole TreeView is being sent as a parameter.第二个仅针对当前的Overlay项目,但整个TreeView正在作为参数发送。

Update: The Overlay class is a third party control and is used in a lot of places that I can't modify.更新: Overlay class 是第三方控件,用于很多我无法修改的地方。 So I can't just add a property to it.所以我不能只给它添加一个属性。

Update2: I've managed to get the Overlay to send properly. Update2:我已经设法让Overlay正确发送。 Just need the IsChecked property now.现在只需要IsChecked属性。

The binding for IsChecked should use {RelativeSource Self} , since the binding is being applied to the CheckBox via the Style . IsChecked的绑定应该使用{RelativeSource Self} ,因为绑定是通过Style应用于CheckBox的。

Your update to your question shows you've already solved the other one.您对问题的更新表明您已经解决了另一个问题。

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

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