没有 XAML 的 WPF 绑定
WPF Binding Without XAML
我有一个带有 "Grid Window" 的 WPF 应用程序。此 window 没有添加 XAML。我创建了一个网格(列和行),然后在每个网格中放置一个矩形,所有这些都在 C# 中。
这允许我在设置 'Stroke' 的位置创建一个网格,并在设置 'Fill' 时显示网格上的位置。
整个格子设置相同,也就是说,如果格子的一部分是红色的,那么整个格子都是红色的。目前,我通过遍历所有矩形并设置 'Stroke' 属性 来设置网格。这工作正常但与大多数其他操作相比似乎非常慢。我想将笔画 属性 绑定到 C# 中的一个变量(除非迭代是一种合理的处理方式)。
我在这里看了很多问题,但大多数想使用 XAML。我下面的代码基于 。没有错误,网格永远不会出现。
// put a rectangle in each square
for (int i = 0; i < x; i++) // for each column
{
for (int j = 0; j < y; j++) // for each row
{
// create a new rectangle with name, height, width, and starting color (transparent)
var rect = new Rectangle()
{
Name = $"rec{(i + 1).ToString("00")}{(j + 1).ToString("00")}", //column 5 row 2 -> rec0502
Height = squareSize,
Width = squareSize,
Fill = _ColorOff
};
// create the binding
var binder = new Binding
{
Source = _GridColor, // Brush that is updated on color change
Path = new PropertyPath("Stroke")
};
// apply the binding to the rectangle
rect.SetBinding(Rectangle.StrokeProperty, binder);
rect.DataContext = binder;
// place the rectangle
Grid.SetColumn(rect, i); // current column
Grid.SetRow(rect, (y - j - 1)); // same row but from the bottom (puts point 0,0 at bottom left)
// add the rectangle to the grid
grdBattleGrid.Children.Add(rect);
}
}
即使迭代很好,我仍然想知道我做错了什么。
编辑:颜色名称是从单独的 window 上的 ComboBox 中选择的。这会更新用户设置,进而引发我的 "Grid Window" 订阅的事件。在遍历矩形之前,我将名称转换为 SolidColorBrush。
最简单的解决方案是根本没有任何绑定。将 _GridColor
分配给矩形的描边。每当(假定的 SolidColorBrush)_GridColor
的颜色 属性 发生变化时,它会影响所有矩形。
public SolidColorBrush _GridColor { get; } = new SolidColorBrush();
...
var rect = new Rectangle
{
Name = $"rec{(i + 1).ToString("00")}{(j + 1).ToString("00")}",
Height = squareSize,
Width = squareSize,
Fill = _ColorOff,
Stroke = _GridColor // here
};
Grid.SetColumn(rect, i);
Grid.SetRow(rect, (y - j - 1));
grdBattleGrid.Children.Add(rect);
通过为 _GridColor
笔刷的 Color
属性 赋值来更改矩形描边:
_GridColor.Color = Colors.Red;
您只需要为 Window
设置一次 DataContext
,而不是每个 Rectangle
。
Binding
您自己的视图模型class来自INotifyPropertyChanged
界面吗? Link to MS Docs
我有一个带有 "Grid Window" 的 WPF 应用程序。此 window 没有添加 XAML。我创建了一个网格(列和行),然后在每个网格中放置一个矩形,所有这些都在 C# 中。 这允许我在设置 'Stroke' 的位置创建一个网格,并在设置 'Fill' 时显示网格上的位置。
整个格子设置相同,也就是说,如果格子的一部分是红色的,那么整个格子都是红色的。目前,我通过遍历所有矩形并设置 'Stroke' 属性 来设置网格。这工作正常但与大多数其他操作相比似乎非常慢。我想将笔画 属性 绑定到 C# 中的一个变量(除非迭代是一种合理的处理方式)。
我在这里看了很多问题,但大多数想使用 XAML。我下面的代码基于
// put a rectangle in each square
for (int i = 0; i < x; i++) // for each column
{
for (int j = 0; j < y; j++) // for each row
{
// create a new rectangle with name, height, width, and starting color (transparent)
var rect = new Rectangle()
{
Name = $"rec{(i + 1).ToString("00")}{(j + 1).ToString("00")}", //column 5 row 2 -> rec0502
Height = squareSize,
Width = squareSize,
Fill = _ColorOff
};
// create the binding
var binder = new Binding
{
Source = _GridColor, // Brush that is updated on color change
Path = new PropertyPath("Stroke")
};
// apply the binding to the rectangle
rect.SetBinding(Rectangle.StrokeProperty, binder);
rect.DataContext = binder;
// place the rectangle
Grid.SetColumn(rect, i); // current column
Grid.SetRow(rect, (y - j - 1)); // same row but from the bottom (puts point 0,0 at bottom left)
// add the rectangle to the grid
grdBattleGrid.Children.Add(rect);
}
}
即使迭代很好,我仍然想知道我做错了什么。
编辑:颜色名称是从单独的 window 上的 ComboBox 中选择的。这会更新用户设置,进而引发我的 "Grid Window" 订阅的事件。在遍历矩形之前,我将名称转换为 SolidColorBrush。
最简单的解决方案是根本没有任何绑定。将 _GridColor
分配给矩形的描边。每当(假定的 SolidColorBrush)_GridColor
的颜色 属性 发生变化时,它会影响所有矩形。
public SolidColorBrush _GridColor { get; } = new SolidColorBrush();
...
var rect = new Rectangle
{
Name = $"rec{(i + 1).ToString("00")}{(j + 1).ToString("00")}",
Height = squareSize,
Width = squareSize,
Fill = _ColorOff,
Stroke = _GridColor // here
};
Grid.SetColumn(rect, i);
Grid.SetRow(rect, (y - j - 1));
grdBattleGrid.Children.Add(rect);
通过为 _GridColor
笔刷的 Color
属性 赋值来更改矩形描边:
_GridColor.Color = Colors.Red;
您只需要为 Window
设置一次 DataContext
,而不是每个 Rectangle
。
Binding
您自己的视图模型class来自INotifyPropertyChanged
界面吗? Link to MS Docs