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));
}
}
}
}
您可以尝试使用IValueConverter将bool转换为您选择的样式。如果您想拥有两种以上的款式,您也可以使用任何其他类型。我喜欢交换资源字典来改变主题。 – Magus
听起来很有希望。你能举个例子吗?我是新来的! –
你的代码全错了。删除所有。你不能在'DependencyObject'中实现'INotifyPropertyChanged'。请阅读[MSDN文档](http://msdn.microsoft.com/en-us/library/ms745025(v = vs.110).aspx)了解如何在WPF中创建控件。 –