简体   繁体   中英

WPF Data binding error on a checkbox within a combobox

Using WPF, I have a checkbox within a combobox and I keep getting a data binding error when trying to bind a command in a checkbox back to my view model. Here's the error

'OnComboMultiSelectCheckedCommand' property not found on 'object' ''String' (HashCode=-66358460)'. BindingExpression:Path=OnComboMultiSelectCheckedCommand;
 DataItem='String' (HashCode=-66358460); 
target element is 'InvokeCommandAction' (HashCode=61927311); 
target property is 'Command' (type 'ICommand')

Here's an excerpt from the XAML

  <ComboBox Name="comboMultiSelectBox" SelectedItem="{Binding TargetValue, UpdateSourceTrigger=LostFocus}">
                <ComboBox.Style>
                    <Style TargetType="ComboBox">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding TargetPropert}" Value="Weather">
                                <Setter Property="ItemsSource" Value="{Binding WeatherList}"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ComboBox.Style>
                <ComboBox.ItemTemplateSelector>
                        <customControls:ComboBoxItemTemplateSelector>
                            <customControls:ComboBoxItemTemplateSelector.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <CheckBox  Content="{Binding}"/>
                                            <i:Interaction.Triggers>
                                                <i:EventTrigger EventName="Checked">
                                                    <i:InvokeCommandAction Command="{Binding OnComboMultiSelectCheckedCommand}"></i:InvokeCommandAction>
                                                </i:EventTrigger>
                                                <i:EventTrigger EventName="Unchecked">
                                            <i:InvokeCommandAction Command="{Binding Path = OnComboMultiSelectUncheckedCommand}"></i:InvokeCommandAction>
                                                </i:EventTrigger>
                                            </i:Interaction.Triggers>        
                                    </StackPanel>
                                </DataTemplate>
                            </customControls:ComboBoxItemTemplateSelector.ItemTemplate>
                            <customControls:ComboBoxItemTemplateSelector.SelectedItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                    <TextBlock Text ="{Binding TextForDisplay}"></TextBlock>
                                    </StackPanel>
                                </DataTemplate>
                            </customControls:ComboBoxItemTemplateSelector.SelectedItemTemplate>
                        </customControls:ComboBoxItemTemplateSelector>
                    </ComboBox.ItemTemplateSelector>
</ComboBox>

I have used a Template selection technique as described in #1012 ( https://wpf.2000things.com/?s=combobox ).

The itemsource for the combo box (WeatherList) is just a list of strings and the binding for the combobox is definitely working. The problem is that the checkbox is not picking up the commands I have defined in the viewmodel and I get the binding error as described above.

Thanks

Several tips and suggestions here:

  1. You are missing the /CheckBox closure
  2. Try using {Binding ElementName=comboMultiSelectBox, Path=OnComboMultiSelectCheckedCommand } .
<CheckBox Content="{Binding}"/>
   <i:Interaction.Triggers>
      <i:EventTrigger EventName="Checked">
         <i:InvokeCommandAction Command="{Binding ElementName=comboMultiSelectBox, Path=OnComboMultiSelectCheckedCommand }" />
      </i:EventTrigger>
      <i:EventTrigger EventName="Unchecked">
         <i:InvokeCommandAction Command="{Binding ElementName=comboMultiSelectBox, Path=OnComboMultiSelectUncheckedCommand }" />
      </i:EventTrigger>
   </i:Interaction.Triggers>
</CheckBox>
  1. Create a viewmodel for each WeatherListItem so events can be handled in this dedicated viewmodel itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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