简体   繁体   English

WPF(.NET-Core):如何以编程方式将按钮背景颜色绑定到矩阵的索引值?

[英]WPF(.NET-Core):How can I bind a buttons background color, to a matrix's indexed value progmatically?

In a WPF .NET-CORE application I want to bind the background color of each button in a dynamic grid to a matrix's indexed value.在 WPF .NET-CORE 应用程序中,我想将动态网格中每个按钮的背景颜色绑定到矩阵的索引值。 I can't seem to get it work, the buttons appear, but they dont have any color.我似乎无法让它工作,按钮出现,但它们没有任何颜色。 Here is my code below:下面是我的代码:

XML 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>

View: This is what the NewGame buttons do, the buttons perfectly appear, but have no color.视图:这就是 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);
            }
        }
    }

ViewModel视图模型

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;
            }
        }
    }
}

Found it out...The notification about the changed property didn't happen.发现了...关于更改的属性的通知没有发生。

Correct ViewModel:正确的视图模型:

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;
            }
        }
    }
}

ViewModelBase视图模型库

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 in the View在视图中绑定

Binding b = new Binding("["+i.ToString()+","+j.ToString()+"]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WPF / XAML中绑定背景颜色? - How can I bind a background color in WPF/XAML? 如何重定向到在.net-core MVC中调用了另一个操作的View - How can I redirect to the View which called another action in .net-core MVC 无法将 POST 请求正文中的 json 绑定到 .net-core 中的 class 参数 - Can't bind json in POST request body to class argument in .net-core 在 WPF 中将颜色绑定到背景 - Bind color to background in WPF 如何使用.net-core和Entity Framework Core和Identity从thenInclude中获取特定列? - How do i get specific columns from a thenInclude using .net-core and Entity Framework Core and Identity? 生成.net核心应用程序后,是否可以向其中添加本地化资源(* .resx)? - Can I add localization resources (*.resx) to a .net-core app after it was built? 如何将自定义颜色绑定到WPF工具包ColorPicker? - How can I bind a custom color to WPF toolkit ColorPicker? .net核心应用程序和mashine.config中的空DbProviderFactories - Empty DbProviderFactories in .net-core application and mashine.config(s) .net核心应用程序如何在不考虑操作系统的情况下读取键盘状态? - How can a .net-core application read the keyboard state, irrespective of the operating system? .net-core依赖注入 - .net-core Dependency Injection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM