【问题标题】:wpf combo box default valuewpf组合框默认值
【发布时间】:2016-11-17 19:23:03
【问题描述】:

当 SelectedValue 为 Null 时,我尝试将 Combo Box 选定项默认设置为 index = 0。数据触发器有什么问题? 错误:SelectedIndex 是不可识别的属性

 <ComboBox x:Name="ACombobox" ItemsSource="{Binding Mode=OneWay, Source={StaticResource AList}}" 
                    DisplayMemberPath="TypeName" SelectedValuePath="TypeName" 
                    SelectedValue="{Binding  AnObj.Type, Mode=TwoWay}"  >
                        <ComboBox.Triggers>
                            <DataTrigger Binding="{Binding}" Value="{x:Null}">
                                <Setter Property="SelectedIndex" Value="0" />
                            </DataTrigger>
                        </ComboBox.Triggers>
                    </ComboBox>

【问题讨论】:

    标签: wpf combobox datatrigger


    【解决方案1】:

    您应该通过创建如下所示的样式触发器来做到这一点

    <ComboBox x:Name="ACombobox" ItemsSource="{Binding Mode=OneWay, Source={StaticResource AList}}" 
                    DisplayMemberPath="TypeName" SelectedValuePath="TypeName" 
                    SelectedValue="{Binding  AnObj.Type, Mode=TwoWay}"  >
        <Style TargetType="ComboBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" Value="{x:Null}">
                    <Setter Property="SelectedIndex" Value="0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox>
    

    错误表明it does not have a qualifying type name 因此通过创建一个样式,当您设置TargetType="ComboBox" 时它适用于组合框


    <ComboBox x:Name="ACombobox" ItemsSource="{Binding AList}"
                DisplayMemberPath="TypeName" SelectedValuePath="TypeName" 
                SelectedValue="{Binding  AnObj.Type, Mode=TwoWay}"   >
      <ComboBox.Resources>
        <Style TargetType="ComboBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedItem}" Value="{x:Null}">
                    <Setter Property="SelectedIndex" Value="0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
      </ComboBox.Resources>
    </ComboBox>
    

    这对我有用。


    静态资源示例

         <Window.Resources>
            <x:Array x:Key="StringList" Type="System:String">
                <System:String>Line 1</System:String>
                <System:String>Line 2</System:String>
                <System:String>Line 3</System:String>
                <System:String>Line 4</System:String>
           </x:Array>       
        </Window.Resources>
        <ComboBox ItemsSource="{StaticResource StringList}" >
          <ComboBox.Resources>
            <Style TargetType="ComboBox">
                <Style.Triggers>
                    <Trigger Property="SelectedItem" Value="{x:Null}">
                        <Setter Property="SelectedIndex" Value="0"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ComboBox.Resources>
      </ComboBox>
    

    【讨论】:

    • Abin:我遇到了一个异常:在使用 Items Source 之前,Items Collection 必须为空。
    • alist 是在 xaml 中定义的静态资源。如果我把这种风格去掉,一切都很好,我不会出错。
    • 把你的风格放在ComboBox.Resources@AbinMathew。无论哪种方式,除了启动时,我仍然无法让 SelectedIndex 工作
    • 有人知道如何让它与静态资源一起工作吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2012-04-07
    相关资源
    最近更新 更多