2

私のコンボボックスは状態のリストにバインドされています。状態には列があり、trueの場合、コンボアイテムの前景色は赤になります。これは正常に機能します。しかし、前景色が赤のコンボボックスアイテムを選択すると、その前景色が失われ、黒に設定されます。誰かが私が間違っていることを指摘するのを手伝ってもらえますか?

<ComboBox Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}},Path=Foreground}"
          DisplayMemberPath="StateName" 
          ItemsSource="{Binding States}" 
          SelectedValue="{Binding Model.StateID}" SelectedValuePath="StateID" >
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="Foreground" Value="{Binding DoesNotParticipate, Converter={StaticResource NonParticipatingConverter}}"/>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
4

1 に答える 1

1

ComboBoxes propに設定したバインディングは、ビジュアル ツリーで型指定された先祖Foregroundを探していComboBoxItemますが、必要なアイテムはComboBox. 特にComboBoxes SelectedItem

編集

項目をモデル オブジェクトにバインドするため、ComboBox.SelectedItems タイプはこのモデル オブジェクト タイプになります。つまり、おそらく - Foreground プロパティがありません。

次のことをお勧めします: DoesNotParticipate はブール値の小道具であるように思われるのでConverter、使用しないでください。代わりに を使用してTriggerください。

<ComboBox Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBoxItem}},Path=Foreground}"
      DisplayMemberPath="StateName" 
      ItemsSource="{Binding States}" 
      SelectedValue="{Binding Model.StateID}" SelectedValuePath="StateID" >
<ComboBox.Style>
    <Style TargetType="ComboBox">
        <Style.Triggers>
           <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.DoesNotParticipate}" Value="True">
                        <Setter Property="Foreground" Value="Red"/>
                    </DataTrigger
      </Style.Triggers>
    </Style>
</ComboBox.Style> 
<ComboBox.ItemContainerStyle>
    <Style TargetType="{x:Type ComboBoxItem}">
      <Style.Triggers>
        <DataTrigger Property={Binding DoesNotParticipate} Value="True">
           <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
于 2012-09-13T08:39:31.327 に答える