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中創建控件。 –