Skip to main content
Removed tag from title, highlighted code, fixed code indentation, improved readability, added tags.
Source Link
thatguy
  • 22.3k
  • 6
  • 33
  • 46

WPF Relay Commands Command Parameter

I am trying to work towith the MVVM principles within a small WPF project using c#C#. I have a List boxListBox that is populated with CheckboxesCheckBoxes created through binding back to the ViewModel. I also have a command bound to the checkboxesCheckBoxes and wish to pass the checkbox ContentCheckBoxes Content as a commandParameterCommandParameter. I was looking atfor something like the belowthis:

UnfortuetlyUnfortunately, because the CheckboxesCheckBoxes are created through a binding I don’t have the element name.

The code for ListBoxListBox / ListBoxItem StyleListBoxItem Style is belowthis:

<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:

Ultimately my aimgoal is to be able to display all the selected items Text Contenttext Contents (Countries in this case) inof 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. I would be grateful for any assistanceCountry.

WPF Relay Commands Command Parameter

I am trying to work to the MVVM principles within a small WPF project using c#. I have a List box 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 checkbox Content as a commandParameter. I was looking at something like the below

Unfortuetly because the Checkboxes are created through a binding I don’t have the element name.

The code for ListBox / ListBoxItem Style is below

<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

Ultimately my aim is to be able to display all the selected items Text Content (Countries in this case) in a text box were each country is separated by a comma. The only thing I am currently missing is the Country. I would be grateful for any assistance.

Relay Commands Command Parameter

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:

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:

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.

added 1822 characters in body
Source Link
StephenH
  • 93
  • 1
  • 10

The code for ListBox / ListBoxItem Style is below

<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 aim is to be able to display all the selected items Text Content (Countries in this case) in a text box were each country is separated by a comma. The only thing I am currently missing is the Country. I would be grateful for any assistance.

Ultimately my aim is to be able to display all the selected items Text Content (Countries in this case) in a text box were each country is separated by a comma. The only thing I am currently missing is the Country. I would be grateful for any assistance.

The code for ListBox / ListBoxItem Style is below

<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 aim is to be able to display all the selected items Text Content (Countries in this case) in a text box were each country is separated by a comma. The only thing I am currently missing is the Country. I would be grateful for any assistance.

Source Link
StephenH
  • 93
  • 1
  • 10

WPF Relay Commands Command Parameter

I am trying to work to the MVVM principles within a small WPF project using c#. I have a List box 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 checkbox Content as a commandParameter. I was looking at something like the below

<Binding ElementName="" Path="Content"/> 

Unfortuetly because the Checkboxes are created through a binding I don’t have the element name.

Ultimately my aim is to be able to display all the selected items Text Content (Countries in this case) in a text box were each country is separated by a comma. The only thing I am currently missing is the Country. I would be grateful for any assistance.