c# - Xamarin.Forms 绑定不适用于嵌套的自定义控件
问题描述
我正在尝试创建一个简单的(数独)网格,它基本上由 9 个自定义(网格)控件组成,其中每个控件都包含 9 个其他自定义(网格)控件。但是,绑定似乎在GridCell控件中不起作用。外部GridBigCell控件似乎工作正常,我可以看到它在调试时绑定到OuterCell属性。
代码非常简单,这可能甚至不是创建这样一个网格的好方法,但在这一点上,我只是好奇这里有什么问题:)
MainPage.xaml:
<Grid>
<controls:GridBigCell Grid.Row="0"
Grid.Column="0"
OuterCell="{Binding Grid[0]}" />
<controls:GridBigCell Grid.Row="0"
Grid.Column="1"
OuterCell="{Binding Grid[1]}" />
<controls:GridBigCell Grid.Row="0"
Grid.Column="2"
OuterCell="{Binding Grid[2]}" />
<controls:GridBigCell Grid.Row="1"
Grid.Column="0"
OuterCell="{Binding Grid[3]}" />
<controls:GridBigCell Grid.Row="1"
Grid.Column="1"
OuterCell="{Binding Grid[4]}" />
<controls:GridBigCell Grid.Row="1"
Grid.Column="2"
OuterCell="{Binding Grid[5]}" />
<controls:GridBigCell Grid.Row="2"
Grid.Column="0"
OuterCell="{Binding Grid[6]}" />
<controls:GridBigCell Grid.Row="2"
Grid.Column="1"
OuterCell="{Binding Grid[7]}" />
<controls:GridBigCell Grid.Row="2"
Grid.Column="2"
OuterCell="{Binding Grid[8]}" />
</Grid>
GridBigCell 控件:
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Sudoku.Controls.GridCell"
x:Name="this">
...
//grid definitions
...
<controls:GridCell Grid.Row="0"
Grid.Column="0"
Cell="{Binding OuterCell[0], Source={x:Reference this}}" />
<controls:GridCell Grid.Row="0"
Grid.Column="1"
Cell="{Binding OuterCell[1], Source={x:Reference this}}" />
<controls:GridCell Grid.Row="0"
Grid.Column="2"
Cell="{Binding OuterCell[2], Source={x:Reference this}}" />
<controls:GridCell Grid.Row="1"
Grid.Column="0"
Cell="{Binding OuterCell[3], Source={x:Reference this}}" />
<controls:GridCell Grid.Row="1"
Grid.Column="1"
Cell="{Binding OuterCell[4], Source={x:Reference this}}" />
<controls:GridCell Grid.Row="1"
Grid.Column="2"
Cell="{Binding OuterCell[5], Source={x:Reference this}}" />
<controls:GridCell Grid.Row="2"
Grid.Column="0"
Cell="{Binding OuterCell[6], Source={x:Reference this}}" />
<controls:GridCell Grid.Row="2"
Grid.Column="1"
Cell="{Binding OuterCell[7], Source={x:Reference this}}" />
<controls:GridCell Grid.Row="2"
Grid.Column="2"
Cell="{Binding OuterCell[8], Source={x:Reference this}}" />
</Grid>
public partial class GridBigCell : Grid
{
public static readonly BindableProperty OuterCellProperty = BindableProperty.Create(
nameof(OuterCell),
typeof(OuterCell),
typeof(GridBigCell));
public OuterCell OuterCell
{
get => (OuterCell)GetValue(OuterCellProperty);
set => SetValue(OuterCellProperty, value);
}
public GridBigCell()
{
InitializeComponent();
}
}
网格单元:
<Grid xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Sudoku.Controls.GridCell"
x:Name="this">
...
<Label Grid.ColumnSpan="3"
Grid.RowSpan="3"
Text="{Binding Cell.Number, Source={x:Reference this}, Mode=TwoWay}" />
</Grid>
public partial class GridCell : Grid
{
public static readonly BindableProperty CellProperty = BindableProperty.Create(
nameof(Cell),
typeof(Cell),
typeof(GridCell));
public Cell Cell
{
get => (Cell)GetValue(CellProperty);
set => SetValue(CellProperty, value);
}
public GridCell()
{
InitializeComponent();
}
}
编辑:型号:
public class GameGrid : BindableBase
{
public OuterCell this[int i]
{
get => OuterCells[i];
set => OuterCells[i] = value;
}
public OuterCell[] OuterCells { get; set; }
public GameGrid()
{
OuterCells = new OuterCell[9];
for (var i = 0; i < 9; i++)
{
OuterCells[i] = new OuterCell();
}
}
}
public class Cell : BindableBase
{
public int Number { get; set; } = 1;
public bool IsSelected { get; set; }
public bool IsInvalid { get; set; }
public Note[] Notes { get; set; }
public Cell()
{
Notes = new Note[9];
for (var i = 0; i < 9; i++)
{
Notes[i] = new Note(i + 1);
}
}
}
public class OuterCell : BindableBase
{
public Cell this[int i]
{
get => Cells[i];
set => Cells[i] = value;
}
public Cell[] Cells { get; set; } = new Cell[9];
public OuterCell()
{
for (var i = 0; i < 9; i++)
{
Cells[i] = new Cell();
}
}
}
PropertyChanged 由 Fody.PropertyChanged 库处理。
解决方案
好吧,设法通过删除 GridBigCell.xaml & cs 并再次创建它来修复它。为什么这行得通?不知道。
推荐阅读
- excel - 类型不匹配:范围公式
- android - Firestore 数据加载后如何使用 LiveData 刷新片段的 UI
- postgresql - 使用 Slick 编写一个相当迟钝的 JSON 查询
- python - 在哪里可以找到有关 pyparsing 模块的文档?
- youtube-analytics-api - 如何仅过滤嵌入视频统计信息的 YouTube Analytics API 请求
- javascript - 如何创建可滚动的覆盖内容?
- matplotlib - 跨文件循环以将来自不同数据集的轮廓叠加到一张 pcolor 地图上
- javascript - 剑道网格插入带有自定义类的新行
- r - 使用 unlist() 后,我的有 63 个观察值的列表变成了一个有 65 个观察值的向量
- typescript - 设置对象各个属性的函数的正确类型?