所以我做了一个自定义控件,包含一个按钮和一个图像。我创造了其中的64个,一个棋盘。
for (int i = 1; i <= 8; i++)
{
for (int j = 1; j <= 8; j++)
{
Square field = new Square(i, j, this);
field.Click += OnFieldClick;
squares[i - 1, j - 1] = field;
squares_list.Add(field);
Square_control fieldBase = new Square_control(field);
this.table.Children.Add(fieldBase);
Grid.SetRow(fieldBase, j - 1);
Grid.SetColumn(fieldBase, i - 1);
}
}
Square 是一个继承自 Button 类的类。它的背景设置在它的构造函数中。
Square_Control
是包含按钮和图像的自定义控件。其构造函数将按钮设置为参数。
多亏了调试器,我发现在字段中正确设置了黑色和白色,fieldBase
对象,但是当我运行程序时,所有按钮都是白色的。我觉得我缺少一些关于 WPF 工作原理的重要知识。
Square
构造函数:
public Square(int row, int col, MainWindow wind)
{
if ((row + col) % 2 == 0)
isWhite = true;
else
isWhite = false;
colName = col;
rowName = row;
if (this.isWhite)
SquareColor = wind.BasicWhite;
else
SquareColor = wind.BasicBlack;
Background = SquareColor;
window = wind;
}
BasicWhite = new SolidColorBrush(new Color()
{
R = 255,
B = 255,
G = 255,
A = 255
});
BasicBlack = new SolidColorBrush(new Color()
{
R = 0,
B = 0,
G = 0,
A = 255
});
Square_control XAML:
<Button x:Name="SQR">
<Image x:Name="FigImage"/>
</Button>
它是构造函数:
public Square_control(Button butt)
{
InitializeComponent();
SQR = butt;
}
此外,我尝试直接在 XAML 中设置Square_Control背景颜色,它奏效了。
你确实做得不好。
首先,您已经有了Square
控件;您需要Square_control
做什么?
你甚至不需要Square
;只要做一个新的UserControl
,把你的Button
和你的Image
放进去。
如果需要进一步的专用化(如 IsWhite
属性(,只需将其作为依赖项属性添加到此UserControl
即可。
只需将所需的参数 (x, y( 传递给其构造函数即可。
你可以从这样的东西开始:
public partial class ChessSquare : UserControl
{
public enum Piece
{
King,
Queen,
Rook,
Bishop,
Knight,
Pawn,
None
}
public readonly SolidColorBrush BasicWhite = new SolidColorBrush(new Color { R = 255, G = 255, B = 255, A = 255 });
public readonly SolidColorBrush BasicBlack = new SolidColorBrush(new Color { R = 0, G = 0, B = 0, A = 255 });
public Boolean IsWhite
{
get { return (Boolean)this.GetValue(IsWhiteProperty); }
set
{
this.SetValue(IsWhiteProperty, value);
this.Background = value ? BasicWhite : BasicBlack;
}
}
public static readonly DependencyProperty IsWhiteProperty = DependencyProperty.Register(
nameof(IsWhite), typeof(Boolean), typeof(ChessSquare), new PropertyMetadata(false, new PropertyChangedCallback(OnIsWhitePropertyChanged)));
public Piece PieceType
{
get { return (Piece)this.GetValue(PieceProperty); }
set { this.SetValue(PieceProperty, value); }
}
public static readonly DependencyProperty PieceProperty = DependencyProperty.Register(
nameof(PieceType), typeof(Piece), typeof(ChessSquare), new PropertyMetadata(Piece.None, new PropertyChangedCallback(OnPieceTypePropertyChanged)));
public ChessSquare(int row, int col)
{
InitializeComponent();
IsWhite = (row + col) % 2 == 0;
}
private static void OnIsWhitePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var square = (ChessSquare)d;
square.Background = (bool)e.NewValue ? square.BasicWhite : square.BasicBlack;
}
private static void OnPieceTypePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// change the image, set it to null is the value is Piece.None
}
}
并最终将this.Background
替换为 this.button.Background
,具体取决于您如何在此用户控件的关联 XAML 中命名内容。