2016-07-14 152 views
0

我试图默认组合框选定的项目以index = 0当SelectedValue为空。数据触发器有什么问题? 错误:SelectedIndex的不认可的财产wpf组合框的默认值

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

回答

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> 
+0

阿斌例子:我有一个例外:项目集合必须是使用项目源之前空。 – TheOne

+0

Alist是一个在xaml中定义的静态资源。如果我采取这种风格一切正常,我没有得到任何错误。 – TheOne

+0

尝试代码时遇到同样的问题。 – Kcvin