【发布时间】:2022-01-17 22:18:50
【问题描述】:
我正在尝试在使用 C# 的小型 WPF 项目中使用 MVVM 原则。
我有一个ListBox,其中填充了CheckBoxes,这是通过绑定回ViewModel而创建的。我还有一个绑定到CheckBoxes 的命令,并希望将CheckBoxes Content 作为CommandParameter 传递。我正在寻找这样的东西:
<Binding ElementName="" Path="Content"/>
很遗憾,因为CheckBoxes 是通过绑定创建的,所以我没有元素名称。
ListBox / ListBoxItem Style 的代码是这样的:
<Style x:Key="CheckBoxListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Multiple"></Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Command="{Binding SelectedItemCommand, Mode=OneWay, Source={StaticResource comd}}">
<CheckBox.CommandParameter>
<MultiBinding Converter="{StaticResource cv}">
<Binding ElementName="" Path="Content"/>
<Binding ElementName="" Path="IsChecked"/>
</MultiBinding>
</CheckBox.CommandParameter>
<ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
它的实现是:
<ListBox Grid.Row="1" Style="{StaticResource CheckBoxListStyle}" Name="lstProducts" ItemsSource="{Binding stampInfo, Mode=OneWay, Source={StaticResource vmStamp}}"
DisplayMemberPath="Country" >
</ListBox>
最终我的目标是能够在文本框中显示所有选定项目的文本Contents(在这种情况下为国家/地区),每个国家/地区用逗号分隔。我目前唯一缺少的是Country。
【问题讨论】:
-
您能否在使用绑定的位置显示您的
CheckBoxes 的代码? -
@thatguy。感谢您的回复,我已修改问题以包含请求的绑定。
标签: wpf xaml data-binding controltemplate relaycommand