简体   繁体   中英

Two dimension int array custom dependencyProperty binding not working

Binding Source

    private int[,] _map = new int[22, 12] 
    {
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    };
    public int[,] Map
    {
        get => _map;
        set => SetProperty(ref _map, value);
    }

DependencyProperty

public static readonly DependencyProperty MapArrayProperty =
        DependencyProperty.Register
        (
            "MapArray",
            typeof(int[,]),
            typeof(MapControl),
            new PropertyMetadata
            (
                null,
                new PropertyChangedCallback(MapArrayChanged)
            )
        );

    private static void MapArrayChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MapControl map = (MapControl)d;
        map.MapChanged();
    }

    public int[,] MapArray
    {
        get
        {
            return (int[,])GetValue(MapArrayProperty);
        }
        set
        {
            SetValue(MapArrayProperty, value);
        }
    }

    private void MapChanged()
    {
        for(int i = 0; i<MapArray.GetLength(0); i++)
        {
            for(int j = 0; j<MapArray.GetLength(1); j++)
            {
                Grid grid = new Grid();
                grid.Background = ColorSeletor(MapArray[i, j]);
                Grid.SetRow(grid, i);
                Grid.SetColumn(grid, j);
                GridMap.Children.Add(grid);
            }
        }
    }

start button binding source

    public ICommand StartCommand
    {
        get;
        set;
    }
    private void RandomBlockSelector()
    {
        IDefaultBlock defaultBlock = null;
        Random r = new Random();
        int selectedblock = r.Next(2, 9);
        switch (selectedblock)
        {
            case 2:
                defaultBlock = new IBlock();
                break;
            case 3:
                defaultBlock = new JBlock();
                break;
            case 4:
                defaultBlock = new LBlock();
                break;
            case 5:
                defaultBlock = new OBlock();
                break;
            case 6:
                defaultBlock = new SBlock();
                break;
            case 7:
                defaultBlock = new TBlock();
                break;
            case 8:
                defaultBlock = new ZBlock();
                break;
        }
        InsertBlock(defaultBlock.BlockArray);
    }

    private void InsertBlock(int[,] blockShape)
    {
        for(int i = 4; i<8; i++)
        {
            for(int j = 1; j<5; j++)
            {
                Map[i, j] = blockShape[i - 4, j - 1];
            }
        }
    }

    private void Start()
    {
        RandomBlockSelector();
    }

If i start app, binding is working

第一个启动画面

but if i click button, binding is not working i think that when i click button to revise array the array must be revise and a property change event must occur to change view

If there is another way to bind a 2D array please let me know or could you find error in my code 我想要点击后的图片按钮点击

This is what should appear in the view after the button is clicked if it works

First the Disclaimer:

WPF, Windows forms and other Desktop application formats are not the right tool for game development. The right tool is anything with a game loop . XNA is slightly dated, but there is a whole list of possible current frontends .

The issue is that your collection does not have Change notification in case a collection value changes. What you would need is a ObservableCollection or other "made for WPF" Collection. A ObservableCollection<int>[] - a array of ObservableCollection instances - is propably the droid you are looking for. But a ObservableCollection<ObservableCollection<int>> might be a better choice (you may want to use aliases to keep those types readable).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM