[英]Send current Item and checkbox value in command parameters
I have a TreeView
setup with a HierarchialDataTemplate
.我有一个带有HierarchialDataTemplate
的TreeView
设置。 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
,它们只是绑定到Overlay
的Name
属性。
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
的当前Overlay
和IsChecked
属性将作为命令参数发送到我的视图模型。
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.