2016-07-14 112 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