【发布时间】:2020-10-17 07:40:03
【问题描述】:
当我聚焦或悬停控件时,我想更改 BorderBrush。
它工作得很好,除非在窗口中我设置了默认的BorderBrush。
在这种情况下,即使我聚焦或悬停控件,BorderBrush 也不会再更改。
我已经有一个解决方案:创建另一个属性以避免直接更改主默认属性,并将主属性绑定到该属性,以获得默认值。
但是我想知道是否有另一种解决方案而不添加无用的属性,并且几乎不需要在每个触发器上复制和粘贴整个模板。
<Style TargetType="{x:Type local:IconTextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:IconTextBox}">
<Grid>
<Image Source="{TemplateBinding Icon}"
HorizontalAlignment="Left"
SnapsToDevicePixels="True"/>
<Border Margin="{TemplateBinding InputMargin}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost"
Margin="0" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="RoyalBlue"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrush" Value="SteelBlue"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="BorderBrush" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
我的解决方案BorderBrushValue:
<Style TargetType="{x:Type local:IconTextBox}"
BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Black"/>
<!-- FIX -->
<Setter Property="BorderBrushValue"
Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}"/>
<!-- FIX -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:IconTextBox}">
<Grid>
<Image Source="{TemplateBinding Icon}"
HorizontalAlignment="Left"
SnapsToDevicePixels="True"/>
<Border Margin="{TemplateBinding InputMargin}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrushValue}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<ScrollViewer x:Name="PART_ContentHost"
Margin="0" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrushValue" Value="RoyalBlue"/>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="BorderBrushValue" Value="SteelBlue"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="BorderBrushValue" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
窗口:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
x:Class="WpfApp.Home"
mc:Ignorable="d"
Title="Home" Height="450" Width="800">
<Grid Background="#FF272727">
<!-- this one, with first style, not works -->
<local:IconTextBox HorizontalAlignment="Left" VerticalAlignment="Top"
Width="200" Height="25" Margin="80,80,0,0"
BorderBrush="#FF1E1E1E"/>
</Grid>
</Window>
【问题讨论】:
标签: wpf triggers binding overriding custom-controls