根据布尔值将样式添加到自定义标签

时间:2013-12-10 15:54:01

标签: c# wpf visual-studio xaml

我希望根据自定义类中突出显示的布尔值向标签(自定义类,继承标签)添加样式。如果布尔值为false,我希望它删除样式,否则添加样式。整个应用程序中都会更改此变量。

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


    }


}

1 个答案:

答案 0 :(得分: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创建中指定的元数据

希望有所帮助