[英]Style control within control on Trigger
I have a LabeledTextBox
in my WPF app that's as simple as can be: 我的WPF应用程序中有一个
LabeledTextBox
,它非常简单:
<Grid x:Name="root">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="{Binding Label}"
FontWeight="Bold"
VerticalAlignment="Bottom"
Margin="5,2,5,0"/>
<TextBox Grid.Row="1"
Text="{Binding Text}"
VerticalAlignment="Top"
Margin="5,0,5,2"/>
</Grid>
I bind all my models to that guy to display. 我将所有模型绑定到该人以显示。 I've successfully implemented
IDataErrorInfo
, and can style the LabeledTextBox
like: 我已经成功实现了
IDataErrorInfo
,并且可以像这样设置LabeledTextBox
样式:
<Style TargetType="controls:LabeledTextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="5"/>
</Trigger>
</Style.Triggers>
</Style>
This all works, and the entire control is bordered in red (obviously). 所有这些都有效,并且整个控件以红色边框(明显)。 What's I'd like is to just manipulate the
TextBox
within the LabeledTextBox
, my end goal being to change the background to a pastel red color. 我想要的只是在
LabeledTextBox
操作TextBox
,我的最终目标是将背景更改为柔和的红色。
How can I access my TextBox
from within the trigger, when the trigger is set on the entire LabeledTextbox
? 将触发器设置在整个
LabeledTextbox
上时,如何从触发器内访问我的TextBox
?
I imagine this is a seemingly simple task, I just can't get the syntax right. 我想象这是一个看似简单的任务,我只是无法正确理解语法。 I'm working in a .NET4.0 environment, if that is relevant.
如果相关的话,我正在.NET4.0环境中工作。
Thanks! 谢谢!
Hi I dont think we can access elements through styles but yes we can refer through ControlTemplate.Triggers and specifying the TargetName Property in Setter. 嗨,我认为我们无法通过样式访问元素,但是可以通过ControlTemplate.Triggers引用并在Setter中指定TargetName属性。
<Style TargetType="{x:Type wpfApplication4:LabelledTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type wpfApplication4:LabelledTextBox}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0"
Margin="5,2,5,0"
VerticalAlignment="Bottom"
FontWeight="Bold"
Text="ergergergegr" />
<TextBox
x:Name="MyTextBox"
Grid.Row="1"
Margin="5,0,5,2"
VerticalAlignment="Top"
Text="gtwererggerg" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="MyTextBox" Property="BorderBrush" Value="Red"/>
<Setter TargetName="MyTextBox" Property="BorderThickness" Value="5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
TextBlock
Background
property to that dependency property. TextBlock
Background
属性绑定到该依赖项属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.