如何解决有没有一种方法可以更改xaml中的TextBox ScrollViewer的前景,而无需更改本地属性?
是否可以在不更改本地主要Foreground属性的情况下,通过触发器或可视状态在xaml中更改自定义Foreground
的{{1}}的方法?
这是通用自定义TextBox的xaml样式,具有随机选择的颜色:
TextBox
我尝试在触发器中使用这些行,但是每个人都失败了(什么都不做):
<Style targettype="{x:Type local:CustomTextBox}">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate targettype="{x:Type local:CustomTextBox}">
<Border x:Name="PART_Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1">
<ScrollViewer x:Name="PART_ContentHost"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
Focusable="False"/>
</Border>
<ControlTemplate.Triggers>
<!-- Can be IsMouSEOver,IsFocused,etc... -->
<Trigger Property="IsMouSEOver" Value="True">
<Setter TargetName="PART_Border"
Property="Background" Value="Green"/>
<Setter TargetName="PART_Border"
Property="BorderBrush" Value="DarkGreen"/>
<!-- The only method I kNow that works is this one
that changes the local property -->
<Setter Property="Foreground" Value="Yellow"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在“按钮”中,可以通过更改父元素的<Setter TargetName="PART_ContentHost" Property="Foreground" Value="Yellow"/>
<Setter TargetName="PART_ContentHost" Property="TextBlock.Foreground" Value="Yellow"/>
<Setter TargetName="PART_ContentHost" Property="TextElement.Foreground" Value="Yellow"/>
<Setter TargetName="PART_Border" Property="TextBlock.Foreground" Value="Yellow"/>
<Setter TargetName="PART_Border" Property="TextElement.Foreground" Value="Yellow"/>
(例如TextBlock.Foreground
的{{1}})来更改前景颜色,但这不适用于TextBoxes。
更改此行的本地属性...
TextBlock.Foreground
...具有以下问题:如果以后更改主要的“前景”属性(例如,从“黑色”更改为“灰色”),触发器将无法再将其更改为“黄色”,例如:
PART_Border
VisualStates甚至不能与主要的Foreground属性进行交互。
所以我不知道有没有其他方法可以在xaml中完成此操作,或者它是wpf的局限性吗?
解决方法
如果您完全覆盖ControlTemplate
怎么办?
<TextBox Text="Hiho" Foreground="Red">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<TextBox Name="TheContent" Text="{TemplateBinding Text}"/>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="TheContent" Property="Foreground" Value="Yellow"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TextBox.Style>
</TextBox>
,
您可以使用Storyboard
为属性设置动画。这将对局部值生效:
<Window ...>
<Window.Resources>
<Style TargetType="{x:Type local:CustomTextBox}">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomTextBox}">
<Border x:Name="PART_Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1">
<ScrollViewer x:Name="PART_ContentHost"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden"
Focusable="False"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PART_Border" Property="Background" Value="Green"/>
<Setter TargetName="PART_Border" Property="BorderBrush" Value="DarkGreen"/>
<Trigger.EnterActions>
<BeginStoryboard x:Name="sb">
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Brushes.Yellow}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sb" />
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel>
<local:CustomTextBox Margin="10" Text="sample text" Foreground="Red" />
</StackPanel>
</Window>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。