2016-07-14 10 views
0

SelectedValueがNullの場合、コンボボックスの選択項目をindex = 0にデフォルト設定しようとしています。データトリガーで何が問題になっていますか? エラー: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

が認識されないエラーが設定したときにit does not have a qualifying type nameはとてもスタイルを作成することによって、それはコンボボックスに適用されることを言います

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

以下のようにあなたがスタイルのトリガーを作成することによって、それを行う必要があります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> 

これは私のために働く。 StaticResource

 <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

ABINと


例:私は、例外が発生しました:アイテムコレクションは、アイテムのソースを使用する前に、空である必要があります。 – TheOne

+0

Alistは、xamlで定義された静的リソースです。私はこのスタイルを取る場合、すべてうまく動作し、私はエラーを取得します。 – TheOne

+0

私はあなたのコードを試して同じ問題が発生します。 – Kcvin

関連する問題