WPF标签通过标记绑定到多个组合框

WPF标签通过标记绑定到多个组合框,wpf,xaml,label,Wpf,Xaml,Label,通常,我会将标签与文本框/组合框一对一地关联起来,这样当组合框有焦点时,我可以装饰标签。。。像这样的 <Label Grid.Row="1" Grid.Column="1" Style="{StaticResource styleLabelTextBlockLeft}" Tag="{Binding ElementName=cboColor, Path=(IsFocused)}" > <TextBlock TextWrapp

通常,我会将标签与文本框/组合框一对一地关联起来,这样当组合框有焦点时,我可以装饰标签。。。像这样的

<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
    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"
    ...
/>

有什么想法吗?谢谢。

您可以使用实现所需逻辑的属性编写一个类,然后将包含相关标签的控件的DataContext绑定到此类。接下来,将此标签的标记绑定到类的属性。

您可以使用实现所需逻辑的属性编写一个类,然后将包含相关标签的控件的DataContext绑定到此类。接下来,将此标签的标记绑定到类的属性。

可以使用多绑定/多值转换器。只需从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>

您可以使用多绑定/多值转换器。只需从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>

如果您想要一个纯xaml解决方案,可以使用StyleDataTriggers。 样式中的标记默认为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>

如果您想要一个纯xaml解决方案,可以使用StyleDataTriggers。 样式中的标记默认为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>

我甚至没有意识到WPF在控件上仍然有Tag属性…我甚至没有意识到WPF在控件上仍然有Tag属性。。。