自定义控件,背景不会改变

时间:2018-03-02 14:55:38

标签: c# wpf frameworks

所以我做了一个自定义控件,包含一个按钮和一个图像。我创造了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中设置背景颜色并且它有效。

1 个答案:

答案 0 :(得分:0)

你确实没有正确地做到这一点。

首先,您已经拥有Square控件;你需要Square_control为什么?

你甚至不需要Square;只需制作一个新的UserControl,然后将ButtonImage放入其中 如果您需要进一步的专业化(如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,具体取决于您在此UserControl的相关XAML中命名内容的方式。