1

通常、Label を TextBoxes/ComboBoxes と 1 対 1 で関連付けて、ComboBox にフォーカスがあるときにラベルを装飾できるようにします。このようなものです。

<Label
    Grid.Row="1"
    Grid.Column="1"
    Style="{StaticResource styleLabelTextBlockLeft}"
    Tag="{Binding ElementName=cboColor, Path=(IsFocused)}"
>
    <TextBlock
        TextWrapping="Wrap">What is your favorite color? 
    </TextBlock>
</Label>
<ComboBox
    x:Name="cboColor"
    Grid.Row="1"
    Grid.Column="3"
    ...
/>

私がやりたいのは、Label のすぐ右にある ComboBox にフォーカスがある場合、または最初の ComboBox の右側にある 2 番目の ComboBox にフォーカスがある場合 (すべて同じ行にある)、Label を強調表示することです。擬似コードは次のとおりです。

<Label
    Grid.Row="1"
    Grid.Column="1"
    Style="{StaticResource styleLabelTextBlockLeft}"
    Tag="{Binding ElementName=cboColorOne, Path=(IsFocused)}"
    Tag="{Binding ElementName=cboColorTwo, Path=(IsFocused)}"
>
    <TextBlock
        TextWrapping="Wrap">What is your favorite color? 
    </TextBlock>
</Label>
<ComboBox
    x:Name="cboColorOne"
    Grid.Row="1"
    Grid.Column="3"
    ...
/>
<ComboBox
    x:Name="cboColorTwo"
    Grid.Row="1"
    Grid.Column="5"
    ...
/>

何か案は?ありがとう。

4

3 に答える 3

2

純粋なxamlソリューションが必要な場合は、スタイルデータトリガーを使用できます。スタイルのタグをデフォルトでfalseに設定してから、フォーカスされたときにタグをtrueに設定する各コンボボックスのトリガーを記述します。

<Label.Style>
   <Style TargetType="Label" BasedOn="{StaticResource styleLabelTextBlockLeft}">
     <Setter Property="Tag" Value="False" />
     <Style.Triggers>
       <DataTrigger Binding="{Binding ElementName=cboColor, Path=(IsFocused)}" Value="True">
         <Setter Property="Tag" Value="True" />
       </DataTrigger>
       <DataTrigger Binding="{Binding ElementName=cboColor2, Path=(IsFocused)}" Value="True">
         <Setter Property="Tag" Value="True" />
       </DataTrigger>
 </Style.Triggers>
   </Style>

于 2012-06-21T22:25:47.110 に答える
1

MultiBinding/MultiValueConverterを使用できます。次のように、IMultiValeConverterからクラスを派生させるだけです。

public class ComboBoxFocusedConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)values[0] || (bool)values[1];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        return new object[0]
    }
}

次に、リソースでそれを参照します。

<....Resources>
    <yournamespace:ComboBoxFocusedConverter x:Key="ComboBoxFocusedConverter" />
</....Resources>

そしてそれを次のように使用します:

<Label
    Grid.Row="1"
     Grid.Column="1"
    Style="{StaticResource styleLabelTextBlockLeft}"
>
    <Label.Tag>
        <MultiBinding Converter="{StaticResource ComboBoxFocusedConverter}">
            <Binding ElementName="cboColorOne" Path="IsFocused" />
            <Binding ElementName="cboColorTwo" Path="IsFocused" />
        </MultiBinding>
    </Label.Tag>
    <TextBlock
        TextWrapping="Wrap">What is your favorite color? 
    </TextBlock>
</Label>
于 2012-06-21T22:23:56.997 に答える
1

必要なロジックを実装するプロパティを持つクラスを作成し、問題の Label を含む Control の DataContext をこの Class にバインドできます。次に、この Label のタグをクラスのプロパティにバインドします。

于 2012-06-21T22:11:05.500 に答える