2013-03-18 124 views
0

我有一个按钮。当它被按下时,按钮颜色应该改变。颜色根据特定条件选择(例如:Ok-Gray,错误 - 红色)。这是一个简单的任务,但我不能解决它:(。。 我试过了。我定义的术语之前点击按钮,并将其存储在属性标记(此按钮)。我没有工作的普通按钮。我重写模板键:。按下按钮 - 按条件更改颜色(WPF)

<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> 

转换只能一次此外,任何属性更改标签不启动工作转换 你有什么建议

+1

标记不是依赖项属性,我想这就是为什么绑定没有更新。 – Andy 2013-03-18 10:58:05

回答

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(); 
     } 
    } 
}