繁体   English   中英

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

[英]WPF Data binding error on a checkbox within a combobox

使用 WPF,我在 combobox 中有一个复选框,当尝试将复选框中的命令绑定回我的视图 model 时,我不断收到数据绑定错误。 这是错误

'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')

这是 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>

我使用了 #1012 ( https://wpf.2000things.com/?s=combobox ) 中描述的模板选择技术。

组合框 (WeatherList) 的 itemsource 只是一个字符串列表,并且 combobox 的绑定肯定有效。 问题是该复选框没有选择我在视图模型中定义的命令,并且如上所述出现绑定错误。

谢谢

这里有几个提示和建议:

  1. 您缺少 /CheckBox 闭包
  2. 尝试使用{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. 为每个 WeatherListItem 创建一个视图模型,以便可以在此专用视图模型本身中处理事件。

暂无
暂无

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

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