【问题标题】:Data Bound ComboBox partially unclickable数据绑定组合框部分不可点击
【发布时间】:2013-09-19 01:03:26
【问题描述】:

我有一些组合框是这样设置的:

    <ComboBox Name="CB_OS" Grid.Row="5" ItemsSource="{Binding OS_Sellection}" SelectedIndex="0" Margin="2" SelectionChanged="OSSelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <ComboBoxItem Content="{Binding Name}" IsSelected="{Binding IsSelected, Mode=TwoWay}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

ComboBox 正确填充了 ComboBoxItems,但单击文本 (Content) 不会选择该项目。我必须单击其他地方,那里没有文本来实际更改所选项目。

如果我把它改成这样:

<ComboBox Name="CB_OS" Grid.Row="5" SelectedIndex="0" Margin="2" SelectionChanged="OSSelectionChanged">
            <ComboBoxItem Content="OOOOOOOOO"/>
            <ComboBoxItem Content="OOOOOOOOO"/>
            <ComboBoxItem Content="OOOOOOOOO"/>
</ComboBox>

效果很好。

OS_Selection 仅包含以下成员:

private string name;
private bool isChecked;
private bool isSelected;

所以我的问题是:如何使整行(项目)可点击?

【问题讨论】:

    标签: wpf data-binding combobox


    【解决方案1】:

    不要将ComboBoxItem 放在ComboBox.ItemTemplate

    改变这个:

         <ComboBox Name="CB_OS" Grid.Row="5" ItemsSource="{Binding OS_Sellection}" SelectedIndex="0" Margin="2" SelectionChanged="OSSelectionChanged">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <ComboBoxItem Content="{Binding Name}" IsSelected="{Binding IsSelected, Mode=TwoWay}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    

    为此:

    <ComboBox Grid.Row="5" Margin="2"
              ItemsSource="{Binding OS_Sellection}"
              DisplayMemberPath="Name"/>
    

    另外,WPF 不绑定到private fields,只绑定到public properties

    【讨论】:

      猜你喜欢
      • 2016-05-02
      • 2012-01-24
      • 1970-01-01
      • 2017-11-16
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      相关资源
      最近更新 更多