我試圖在單擊按鈕時更改按鈕的顏色。但它不會改變顏色。這是我的代碼。
private SolidColorBrush[] _btnBrush;
public SolidColorBrush[] btnBrush
{
get
{
return _btnBrush;
}
set
{
_btnBrush = value; OnPropertyChanged("btnBrush");
}
}
public MineViewModel()
{
for (int i = 0; i < 100; i )
btnBrush[i] = (SolidColorBrush)new BrushConverter().ConvertFrom("#EED6C4");
}
按鈕系結的方法
public void LeftClick(string id_no)
{
btnBrush[Convert.ToInt32(id_no)] = (SolidColorBrush)new BrushConverter().ConvertFrom("#B3541E");
}
對于 XAML
<Button x:Name="btn4" Background="{Binding btnBrush[3]}" Command="{Binding LeftClickCommand}" CommandParameter="3"/>
這是正確執行的,陣列中的元素也得到了顏色。但它不會顯示在 UI 上。如果我用單個實心刷子系結它,它就可以作業。但不是用刷子陣列。有什么幫助嗎?
uj5u.com熱心網友回復:
表達方式
btnBrush[i] = (SolidColorBrush)new BrushConverter().ConvertFrom("#B3541E");
不會改變btnBrush屬性的值。它甚至不呼叫屬性設定器。
Color但是,您可以在元素 index 處更改 SolidColorBrush 的屬性i,例如
btnBrush[i].Color = (Color)new ColorConverter().ConvertFrom("#B3541E");
這將通知 UI,因為Color它是一個依賴屬性。否則,陣列元素型別必須實作 INotifyPropertyChanged 介面并觸發更改通知。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424235.html
