[英]Control template trigger not firing correctly after property change in code behind [WPF]
我正在制作元素周期表wpf应用程序,并按如下所示制作了按钮:
<!-- RoundedButton.xaml -->
<Style x:Key="ButtonFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Border>
<Rectangle SnapsToDevicePixels="true" Margin="4" Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="RoundedButton" TargetType="{x:Type Button}" x:Name="butt">
<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="0,0,1,1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="12,12,12,12" BorderThickness="3,3,3,3" RenderTransformOrigin="0.5,0.5" x:Name="border" BorderBrush="Transparent">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Border.RenderTransform>
<Border Background="{TemplateBinding Background}" CornerRadius="12,12,12,12" x:Name="bordertrue">
<Grid>
<Border Opacity="0" x:Name="Shine" Width="Auto" Height="Auto" CornerRadius="12,12,10,10" Margin="0,0,0,0">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Indigo" Offset="0"/>
<GradientStop Color="Crimson" Offset="0.5"/>
<GradientStop Color="Crimson" Offset="0.5"/>
<GradientStop Color="Indigo" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<ContentPresenter VerticalAlignment="Center" Grid.RowSpan="2" HorizontalAlignment="Center" x:Name="contentPresenter"/>
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" TargetName="border" Value="1"/>
<Setter Property="Opacity" TargetName="contentPresenter" Value="0.5"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="RenderTransform" TargetName="border">
<Setter.Value>
<TransformGroup>
<ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
<SkewTransform AngleX="0" AngleY="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" TargetName="Shine" Value="1"/>
<Setter Property="BorderBrush" TargetName="border" Value="Gold"/>
<Setter Property="Foreground" Value="Gold" />
<Setter Property="FontWeight" Value="ExtraBold"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这在mouseover上完美运行。 但是在我的应用程序中,我将文本框和列表框组合在一起,这使我成为搜索框。 每当我搜索特定元素时,我都希望像上面样式中的鼠标悬停一样具有“发光按钮”效果
我试图这样做:
private void highlight(string elementName, bool MultiOrSingle)
{
LinearGradientBrush gradient1 = new LinearGradientBrush();
gradient1.StartPoint = new Point(0.5, 0);
gradient1.EndPoint = new Point(0.5, 1);
gradient1.GradientStops.Add(new GradientStop(Colors.Indigo, 0));
gradient1.GradientStops.Add(new GradientStop(Colors.Crimson, 0.5));
gradient1.GradientStops.Add(new GradientStop(Colors.Crimson, 0.5));
gradient1.GradientStops.Add(new GradientStop(Colors.Indigo, 1));
if (MultiOrSingle == true)
{
foreach (Button elementButton in VisualChildren.FindVisualChildren<Button>(this))
{
if (listBox.Items.Contains(elementButton.Name) == true)
{
elementButton.Background = gradient1;
elementButton.FontWeight = FontWeights.Bold;
elementButton.Foreground = Brushes.Gold;
elementButton.BorderBrush = Brushes.Gold;
}
else
{
elementButton.Background = previousBackgroundColors[elementButton.Name];
elementButton.Foreground = previousForegroundColors[elementButton.Name];
}
}
foreach (Button elementButton in VisualChildren.FindVisualChildren<Button>(this))
{
if (elementButton.Name != "play_quiz" &&
elementButton.Name != "show_scoreboard" &&
elementButton.Name != "update" &&
elementButton.Name != "DragDropGames" &&
listBox.Items.Contains(elementButton.Name) == false)
{
elementButton.Background = Brushes.Gainsboro;
elementButton.BorderBrush = Brushes.DarkBlue;
elementButton.FontWeight = FontWeights.Normal;
elementButton.Foreground = Brushes.Black;
}
}
}
else
{
foreach (Button elementButton in VisualChildren.FindVisualChildren<Button>(this))
{
if (elementName == elementButton.Name)
{
elementButton.Background = gradient1;
elementButton.BorderBrush = Brushes.Gold;
elementButton.FontWeight = FontWeights.Bold;
elementButton.Foreground = Brushes.Gold;
}
else
{
elementButton.Background = previousBackgroundColors[elementButton.Name];
elementButton.Foreground = previousForegroundColors[elementButton.Name];
}
}
foreach (Button elementButton in VisualChildren.FindVisualChildren<Button>(this))
{
if (elementButton.Name != "play_quiz" &&
elementButton.Name != "show_scoreboard" &&
elementButton.Name != "update" &&
elementButton.Name != "DragDropGames" &&
elementName != elementButton.Name)
{
elementButton.Background = Brushes.Gainsboro;
elementButton.BorderBrush = Brushes.DarkBlue;
elementButton.FontWeight = FontWeights.Normal;
elementButton.Foreground = Brushes.Black;
}
}
}
只需设置背景画笔等...。
但是我遇到了2个问题。 当在后面的代码中执行此操作时,我可以更改所有内容,但我的边框笔刷将保持不变,因为它以xaml样式确定,我无法在后面的代码中更改它,不知道为什么? 在调用此函数后,当我将鼠标悬停在按钮上时,它们将发光……但是属性前景不会变为金色? 它将保持黑色
展示 :
这是我的鼠标悬停在“ Shine”上。 当我将鼠标移到按钮上时,它将改变,就像这样
所以这是我的问题,我一直在搜索H..it,结果很少,然后从上方将函数称为“ multi”,然后选择了所有结果
但这对“ elementButton.BorderBrush = Brushes.Gold”无效,它在xaml中保持透明。 突然我的鼠标悬停效果不再一样了,它是<Setter Property="Foreground" Value="Gold" /> <Setter Property="FontWeight" Value="ExtraBold"/>
函数调用后似乎没有触发
把它们加起来。 我希望所有文本框搜索的按钮外观和闪光效果相同
您的Trigger
更改名为“ Border
”的元素上的BorderBrush
。 但是,在后面的代码中,您需要在按钮上设置BorderBrush
,这无济于事,因为您没有在模板的任何位置使用属性BorderBrush
。 你需要做的是TemplateBinding
按钮的BorderBrush
属性模板,如下所示:
<Border CornerRadius="12,12,12,12"
BorderThickness="3,3,3,3"
RenderTransformOrigin="0.5,0.5"
x:Name="border"
BorderBrush="{TemplateBinding BorderBrush}">
当在后面的代码中设置属性时,为了使FontWeight
和Foreground
在鼠标悬停在按钮上时触发,以下是一种可能的解决方案。
在ContentPresenter
设置TextBlock
属性,然后使用TemplateBinding
设置值。
<ContentPresenter TextBlock.Foreground="{TemplateBinding Foreground}"
TextBlock.FontWeight="{TemplateBinding FontWeight}"
VerticalAlignment="Center"
Grid.RowSpan="2"
HorizontalAlignment="Center"
x:Name="contentPresenter"/>
<Setter Property="TextBlock.Foreground" TargetName="contentPresenter" Value="White" />
<Setter Property="TextBlock.FontWeight" TargetName="contentPresenter" Value="ExtraBold"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.