简体   繁体   English

WPF combobox 中的复选框上的数据绑定错误

[英]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.使用 WPF,我在 combobox 中有一个复选框,当尝试将复选框中的命令绑定回我的视图 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这是 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 ).我使用了 #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.组合框 (WeatherList) 的 itemsource 只是一个字符串列表,并且 combobox 的绑定肯定有效。 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您缺少 /CheckBox 闭包
  2. Try using {Binding ElementName=comboMultiSelectBox, Path=OnComboMultiSelectCheckedCommand } .尝试使用{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.为每个 WeatherListItem 创建一个视图模型,以便可以在此专用视图模型本身中处理事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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