I am trying to work with the MVVM principles within a small WPF project using C#. I have a ListBox that is populated with CheckBoxes created through binding back to the ViewModel. I also have a command bound to the CheckBoxes and wish to pass the CheckBoxes Content as a CommandParameter. I was looking for something like this:
<Binding ElementName="" Path="Content"/> Unfortunately, because the CheckBoxes are created through a binding I don’t have the element name.
The code for ListBox / ListBoxItem Style is this:
<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> And its implementation is:
<ListBox Grid.Row="1" Style="{StaticResource CheckBoxListStyle}" Name="lstProducts" ItemsSource="{Binding stampInfo, Mode=OneWay, Source={StaticResource vmStamp}}" DisplayMemberPath="Country" > </ListBox> Ultimately my goal is to be able to display the text Contents (Countries in this case) of all the selected items in a text box were each country is separated by a comma. The only thing I am currently missing is the Country.
CheckBoxes where the binding is used?