2013-12-10 55 views
1

我想根据自定义类中的布尔高亮显示将样式添加到标签(自定义类,继承标签)。如果布尔值为false,我希望它删除样式,否则添加样式。这个变量在整个应用程序中都被改变根据布尔值将样式添加到自定义标签

(风格:SelectedBackground)

<local:unit xPos="13" yPos="1" Grid.Row="{Binding yPos, RelativeSource={RelativeSource Self}}" Grid.Column="{Binding xPos, RelativeSource={RelativeSource Self}}" /> 

单位:

public class Unit : Label, INotifyPropertyChanged 
    { 
     public Unit() { } //Grass 
     public Unit(int x, int y) 
     { 
      this.xPos = x; 
      this.yPos = y; 
     } 

     public static readonly RoutedEvent ClickEvent; 

     private int _xPos, _yPos; 

     public bool _highlighted = false; 
     public bool highlighted 
     { 
      get { return _highlighted; } 
      set { 
       _highlighted = value; 
       NotifyPropertyChanged("highlighted"); 
      } 
     } 

     public bool mouseLeft 
     { 
      get { return _mouseLeft; } 
      set 
      { 
       _mouseLeft = value; 
      } 
     } 
     public bool mouseRight 
     { 
      get { return _mouseRight; } 
      set 
      { 
       _mouseRight = value; 
      } 
     } 

     public int xPos 
     { 
      get { return _xPos; } 
      set 
      { 
       _xPos = value; 
       NotifyPropertyChanged("xPos"); 
      } 
     } 
     public int yPos 
     { 
      get { return _yPos; } 
      set 
      { 
       _yPos = value; 
       NotifyPropertyChanged("yPos"); 
      } 
     } 

     private string _type = "none"; 
     public string type 
     { 
      get { return _type; } 
      set 
      { 
       _type = value; 
      } 

     } 


     static Unit() 
     { 
      ClickEvent = ButtonBase.ClickEvent.AddOwner(typeof(Unit)); 
     } 



     public event PropertyChangedEventHandler PropertyChanged; 

     public void NotifyPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 

       System.Diagnostics.Debug.WriteLine("property changed"); 

       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 


    } 


} 
+0

您可以尝试使用IValueConverter将bool转换为您选择的样式。如果您想拥有两种以上的款式,您也可以使用任何其他类型。我喜欢交换资源字典来改变主题。 – Magus

+0

听起来很有希望。你能举个例子吗?我是新来的! –

+0

你的代码全错了。删除所有。你不能在'DependencyObject'中实现'INotifyPropertyChanged'。请阅读[MSDN文档](http://msdn.microsoft.com/en-us/library/ms745025(v = vs.110).aspx)了解如何在WPF中创建控件。 –

回答

1

您需要应用样式与被绑定到属性标志的视图模型数据触发。

这里是XAML:

<wpfApp:CustomLabel> 
    <wpfApp:CustomLabel.Style> 
     <Style TargetType="{x:Type wpfApp:CustomLabel}"> 
      <Style.Triggers> 
      <DataTrigger Binding="{Binding ApplyStyleToLabel}" Value="True"> 
       <Setter Property="Foreground" Value="Blue"/> 
       <!-- Specify the remaining property setters here --> 
      </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </wpfApp:CustomLabel.Style> 
</wpfApp:CustomLabel> 

应指定在给定的位置,其余制定者。从某种意义上说,您希望通过样式进行的更改仅适用于属性为true时的标签。您也可以使用自己的属性来绑定数据触发器。

你不必获得来自INotifyPropertyChanged的 控件在控件创建一个依赖属性,这里将是你的XAML

<wpfApp:CustomLabel> 
      <wpfApp:CustomLabel.Style> 
      <Style TargetType="{x:Type wpfApp:CustomLabel}"> 
       <Style.Triggers> 
         <Trigger Property="ApplyStyle" Value="True"> 
          <Setter Property="Foreground" Value="Blue"/> 
         </Trigger> 
       </Style.Triggers> 
      </Style> 
     </wpfApp:CustomLabel.Style> 
</wpfApp:CustomLabel> 

控制类如下:

public class CustomLabel : Label 
{ 


    public bool ApplyStyle 
    { 
     get { return (bool)GetValue(ApplyStyleProperty); } 
     set { SetValue(ApplyStyleProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for ApplyStyle. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ApplyStyleProperty = 
     DependencyProperty.Register("ApplyStyle", typeof(bool), typeof(CustomLabel), new PropertyMetadata(false)); 


} 

了解更多关于创建依赖项属性here 这应该给你一个关于在dp创建中指定元数据的想法

希望它有帮助

相关问题