二维 int 数组自定义 dependencyProperty 绑定不起作用
Two dimension int array custom dependencyProperty binding not working
绑定源
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);
}
依赖属性
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);
}
}
}
开始按钮绑定源码
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();
}
如果我启动应用程序,则绑定正常
但是如果我点击按钮,绑定不起作用
我想当我点击按钮修改数组时
必须修改数组和 属性 更改事件
必须发生才能更改视图
如果有其他绑定二维数组的方法请告诉我
要么
你能在我的代码中找到错误吗
单击按钮后,如果它有效,视图中应该显示此内容
首先是免责声明:
WPF、Windows 表单和其他桌面应用程序格式不是游戏开发的正确工具。正确的工具是带有 game loop. XNA is slightly dated, but there is a whole list of possible current frontends.
的任何东西
问题是您的 collection 没有更改通知以防 collection 值发生变化。您需要的是 ObservableCollection 或其他 "made for WPF" Collection。一个 ObservableCollection<int>[]
- 一组 ObservableCollection 实例 - 可能就是您正在寻找的机器人。但是 ObservableCollection<ObservableCollection<int>>
可能是更好的选择(您可能希望使用别名来保持这些类型的可读性)。
绑定源
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);
}
依赖属性
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);
}
}
}
开始按钮绑定源码
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();
}
如果我启动应用程序,则绑定正常
但是如果我点击按钮,绑定不起作用 我想当我点击按钮修改数组时 必须修改数组和 属性 更改事件 必须发生才能更改视图
如果有其他绑定二维数组的方法请告诉我
要么
你能在我的代码中找到错误吗
单击按钮后,如果它有效,视图中应该显示此内容
首先是免责声明:
WPF、Windows 表单和其他桌面应用程序格式不是游戏开发的正确工具。正确的工具是带有 game loop. XNA is slightly dated, but there is a whole list of possible current frontends.
的任何东西问题是您的 collection 没有更改通知以防 collection 值发生变化。您需要的是 ObservableCollection 或其他 "made for WPF" Collection。一个 ObservableCollection<int>[]
- 一组 ObservableCollection 实例 - 可能就是您正在寻找的机器人。但是 ObservableCollection<ObservableCollection<int>>
可能是更好的选择(您可能希望使用别名来保持这些类型的可读性)。