DataGridTemplateColumn行悬停文本框前景(DataGridTemplateColumn row hover textbox foreground)

编程入门 行业动态 更新时间:2024-10-06 17:21:12
DataGridTemplateColumn行悬停文本框前景(DataGridTemplateColumn row hover textbox foreground)

我有一个DataGridTemplateColumn,里面有一个文本框。 当我将鼠标悬停在该行上时,除文本框外,所有其他前景变为白色。 我可以对文本框应用什么样的样式,使其前景更改为当鼠标悬停在行上时与其他文本块/字体相匹配?

示例XAML

<DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <TextBox Text="Test 123" /> <TextBlock Text="Test 123" /> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>

造型

<Style TargetType="{x:Type DataGridRow}"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="{x:Null}" /> <Setter Property="Foreground" Value="{StaticResource BlackBrush}" /> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="{x:Null}" /> <Setter Property="Foreground" Value="{StaticResource BlackBrush}" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{StaticResource RedBrush}" /> <Setter Property="Foreground" Value="{StaticResource WhiteBrush}" /> </Trigger> </Style.Triggers> </Style> <Style TargetType="{x:Type DataGridCell}"> <Setter Property="Padding" Value="5, 10" /> <Setter Property="IsTabStop" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridCell}"> <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="{Binding Path=Background, RelativeSource={RelativeSource AncestorType=DataGridRow}}" /> <Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=DataGridRow}}" /> <Setter Property="BorderBrush" Value="{Binding Path=BorderBrush, RelativeSource={RelativeSource AncestorType=DataGridRow}}" /> </Trigger> </Style.Triggers> </Style>

I have a DataGridTemplateColumn with a textbox inside of it. When I hover on the row, all other Foregrounds turn white except the textbox. What kind of styling can I apply to the textbox to make it's foreground change to match the other textblocks/fonts when the mouse is hovering over the row?

Example XAML

<DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <TextBox Text="Test 123" /> <TextBlock Text="Test 123" /> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>

Styling

<Style TargetType="{x:Type DataGridRow}"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="{x:Null}" /> <Setter Property="Foreground" Value="{StaticResource BlackBrush}" /> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="Transparent" /> <Setter Property="BorderBrush" Value="{x:Null}" /> <Setter Property="Foreground" Value="{StaticResource BlackBrush}" /> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="{StaticResource RedBrush}" /> <Setter Property="Foreground" Value="{StaticResource WhiteBrush}" /> </Trigger> </Style.Triggers> </Style> <Style TargetType="{x:Type DataGridCell}"> <Setter Property="Padding" Value="5, 10" /> <Setter Property="IsTabStop" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridCell}"> <Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="{Binding Path=Background, RelativeSource={RelativeSource AncestorType=DataGridRow}}" /> <Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=DataGridRow}}" /> <Setter Property="BorderBrush" Value="{Binding Path=BorderBrush, RelativeSource={RelativeSource AncestorType=DataGridRow}}" /> </Trigger> </Style.Triggers> </Style>

最满意答案

您需要为TextBox定义一个额外的Style :

<Style TargetType="TextBox"> <Style.Triggers> <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="True"> <Setter Property="Foreground" Value="White" /> </DataTrigger> </Style.Triggers> </Style>

You need to define an additional Style for the TextBox:

<Style TargetType="TextBox"> <Style.Triggers> <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="True"> <Setter Property="Foreground" Value="White" /> </DataTrigger> </Style.Triggers> </Style>

更多推荐

Property,<Setter,Value,电脑培训,计算机培训,IT培训"/> <meta name="des

本文发布于:2023-07-19 15:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   前景   DataGridTemplateColumn   row   foreground

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!