我有一个按钮。按下时,按钮颜色应该改变。根据特定条件选择颜色(例如:Ok-Gray,error - Red)。这是一项简单的任务,但我无法解决它:(。 我试过的。我在单击按钮之前定义术语并将其存储在属性Tag(此按钮)中。我没有为普通按钮工作。我重写了模板按钮:
<Style x:Key="SimpleButton" TargetType="{x:Type Button}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="MinHeight" Value="23" />
<Setter Property="MinWidth" Value="75" />
<Setter Property="Tag" Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border"
Background="#C0C0C0"
BorderBrush="#404040"
BorderThickness="1"
CornerRadius="2">
<ContentPresenter Margin="0,6,27,6"
HorizontalAlignment="Right"
VerticalAlignment="Center"
RecognizesAccessKey="True" />
</Border>
<ControlTemplate.Triggers>
<!-- !!!!! -->
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border" Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag, Converter={StaticResource StatusToBrushConverter}}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter TargetName="Border" Property="BorderBrush" Value="#202020" />
</Trigger>
<Trigger Property="IsDefaulted" Value="true">
<Setter TargetName="Border" Property="BorderBrush" Value="#202020" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
<Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
<Setter Property="Foreground" Value="#888888" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
转换器仅工作一次。此外,任何属性更改标记不启动工作转换器。 你有什么建议吗?
答案 0 :(得分:1)
我可能会使用附加的依赖属性而不是标记,类似这样的
<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:test"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="local:ButtonThing.Tag2" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ControlTemplate.Triggers>
<Trigger Property="local:ButtonThing.Tag2" Value="1">
<Setter Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="local:ButtonThing.Tag2" Value="2">
<Setter Property="Background" Value="Blue"/>
</Trigger>
</ControlTemplate.Triggers>
<Grid Background="{TemplateBinding Background}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button local:ButtonThing.Tag2="1"/>
</Grid>
</Window>
背后的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace test
{
public class ButtonThing : DependencyObject
{
public static object GetTag2(DependencyObject obj)
{
return (object)obj.GetValue(Tag2Property);
}
public static void SetTag2(DependencyObject obj, object value)
{
obj.SetValue(Tag2Property, value);
}
public static readonly DependencyProperty Tag2Property = DependencyProperty.RegisterAttached("Tag2", typeof(object), typeof(ButtonThing), new PropertyMetadata(null));
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}