2

我想同步两个TextBlockes的背景。例如,如果鼠标在任何文本块上,我想将两个文本块的背景颜色更改为相同。我知道我可以使用这个触发器来改变一个背景:

    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="LightGray"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Gray"/>
            </Trigger>
        </Style.Triggers>
    </Style>

但是如何同步它们?

更新:这也不起作用:

<UserControl.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="LightGray"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Gray"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Column="0" Background="{Binding Path=Background, ElementName=tbHeader, Mode=TwoWay}" Text="A"/>
    <TextBlock x:Name="tbHeader" Grid.Column="1" Text="B"/>
</Grid>
4

3 回答 3

0

如果您正在寻找“正确”的 MVVM 方式,则应将鼠标悬停事件/属性绑定到视图模型的同一属性,并将背景再次绑定到同一属性,并使用转换器将其转换为适当的颜色(或带有模板的样式)。

于 2012-11-26T19:47:25.257 回答
0

您可以通过将它们放入ContentControl并像这样应用触发器来同步它ControlTemplate-

  <Grid>
    <ContentControl>
        <ContentControl.Template>
            <ControlTemplate>
                <StackPanel>
                    <TextBlock Text="Rohit" x:Name="txt1"/>
                    <TextBlock Text="Vats" x:Name="txt2"/>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="TextBlock.IsMouseOver" SourceName="txt1" 
                             Value="True">
                        <Setter Property="TextBlock.Background" Value="Red" 
                                TargetName="txt1"/>
                        <Setter Property="TextBlock.Background" Value="Red" 
                                TargetName="txt2"/>
                    </Trigger>
                    <Trigger Property="TextBlock.IsMouseOver" SourceName="txt2" 
                             Value="True">
                        <Setter Property="TextBlock.Background" Value="Red" 
                                TargetName="txt1"/>
                        <Setter Property="TextBlock.Background" Value="Red" 
                                TargetName="txt2"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ContentControl.Template>
    </ContentControl>
  </Grid>
于 2012-11-26T19:44:44.220 回答
0

尝试元素到元素绑定

<TextBlock x:Name=textBlock1>
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="LightGray"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Gray"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</TextBlock.Style>
</TextBlock>

<TextBlock Background={Binding Path=Background ElementName=textBlock1/><!--Background gets set from textBlock1-->
于 2012-11-26T16:28:42.490 回答