c# - WPF(.NET-Core):如何以编程方式将按钮背景颜色绑定到矩阵的索引值?
问题描述
在 WPF .NET-CORE 应用程序中,我想将动态网格中每个按钮的背景颜色绑定到矩阵的索引值。我似乎无法让它工作,按钮出现,但它们没有任何颜色。下面是我的代码:
XML
<Grid>
<DockPanel >
<Menu Height="24" DockPanel.Dock="Top">
<MenuItem Name ="smallGameButton" Header="4x16" Click="NewGame"/>
<MenuItem Name ="mediumGameButton" Header="8x16" Click="NewGame"/>
<MenuItem Name ="largeGameButton" Header="12x16" Click="NewGame"/>
</Menu>
<Grid Name="GameTable"/>
</DockPanel>
</Grid>
视图: 这就是 NewGame 按钮的作用,按钮完美显示,但没有颜色。
public void NewGame(object sender, RoutedEventArgs e)
{
string buttonName = (sender as MenuItem).Name;
switch (buttonName)
{
case "smallGameButton":
InitGame(4, 16);
break;
case "mediumGameButton":
InitGame(8, 16);
break;
case "largeGameButton":
InitGame(12, 16);
break;
default:
break;
}
}
private void InitGame(int width,int height)
{
//RESET GRID
GameTable.ColumnDefinitions.Clear();
GameTable.RowDefinitions.Clear();
GameTable.Children.Clear();
_viewModel = new GameTableViewModel(width,height);
DataContext = _viewModel;
Width =30*width + 20;
Height = 30 * height + 70;
for(int i = 0; i < width; i++)
{
ColumnDefinition CDef = new ColumnDefinition();
CDef.Width = new GridLength(30, GridUnitType.Pixel);
GameTable.ColumnDefinitions.Add(CDef) ;
}
for(int i = 0; i < height; i++)
{
RowDefinition RDef = new RowDefinition();
RDef.Height = new GridLength(30, GridUnitType.Pixel);
GameTable.RowDefinitions.Add(RDef);
}
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
Button button = new Button();
Grid.SetRow(button, i);
Grid.SetColumn(button, j);
Binding b = new Binding("Matrix["+i.ToString()+","+j.ToString()+"]");
b.Source = _viewModel;
button.SetBinding(Button.BackgroundProperty, b);
GameTable.Children.Add(button);
}
}
}
视图模型
class GameTableViewModel
{
public Brush[,] Matrix;
public Brush Color = Brushes.Red;
public GameTableViewModel(int width,int height)
{
Matrix = new Brush[height, width];
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
Matrix[i, j] = Brushes.Blue;
}
}
}
}
解决方案
发现了...关于更改的属性的通知没有发生。
正确的视图模型:
class GameTableViewModel : ViewModelBase
{
private Brush[,] _matrix;
public Brush this[int height,int width]
{ get=>_matrix[height,width];
set
{
_matrix[height,width] = value;
OnPropertyChanged();
}
}
public Brush Color = Brushes.Red;
public GameTableViewModel(int width,int height)
{
_matrix = new Brush[height, width];
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
_matrix[i, j] = Brushes.Blue;
}
}
}
}
视图模型库
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
在视图中绑定
Binding b = new Binding("["+i.ToString()+","+j.ToString()+"]");
推荐阅读
- c++ - 如何正确调用命令缓冲区的 vulkan.hpp 构造函数?
- python - 服务器无法对整数数组进行排序并发送给客户端
- node.js - 在 Express JS 中 app.listen 是否需要位于文件末尾?
- c# - C# - 我如何转换 KeyValuePair
到键值对 - ios - 用于所有单元的第二个缓存视频?
- php - 我们可以使用 $_SERVER['REQUEST_TIME_FLOAT'] 来获得可靠的处理时间吗?
- c++ - 如何在另一个类中更新一个类的二维数组成员?
- asp.net-core - 可以在 IIS 下的 .Net 4.x 中调试 Kestrel 和 .NET Core 中的 HTTP 和 HTTPS
- r - R:计算分组数据帧的第一行和当前行之间的距离
- javascript - 根据在另一个 API 上重新收集的数据从一个 API 请求数据