Xamarin.Forms 绑定不适用于嵌套的自定义控件

想法

我正在尝试创建一个简单的(数独)网格,它基本上由 9 个自定义(网格)控件组成,其中每个控件都包含 9 个其他自定义(网格)控件。但是绑定似乎在GridCell控件中不起作用外部GridBigCell控件似乎工作正常,我可以看到它在调试时绑定到OuterCell属性。

代码非常简单,这甚至可能不是创建这样一个网格的好方法,但此时我只是好奇这里的问题是什么:)

主页.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 并重新创建它来修复它。为什么这有效?不知道。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Xamarin.Forms:颜色资源不适用于自定义控件的颜色属性

Xamarin.Forms FontAwesome不适用于绑定属性

Xamarin.Forms 编译绑定不适用于 DataTemplate

Xamarin Forms自定义控件数据绑定问题

自定义控件中的Xamarin.Forms绑定集合

自定义Xamarin表单控件的数据绑定失败,但适用于普通控件

Xamarin表单的ReactiveUI:双向绑定不适用于自定义BindableProperty

Xamarin样式不适用于自定义视图

自定义控件的Xamarin数据绑定值评估为Xamarin.Forms.Binding

ReactiveUI版本9.19.5绑定不适用于Xamarin.Forms版本4.1.0

轮播视图和列表视图绑定不适用于Xamarin Forms

GestureRecognizer不适用于ios xamarin.forms

新的Xamarin Forms 4.6 MediaElement不适用于Android

Xamarin Forms 自定义控件和可绑定属性无法按预期工作

ReactiveUI中的Xamarin.Forms控件是否需要自定义绑定?

Xamarin Forms中的自定义控件不起作用

[Xamarin.Forms] ListView中的自定义控件

在Xamarin.Forms中绑定自定义视图

在 Xamarin Forms 中绑定自定义条目

Xamarin.Forms中的自定义嵌套ListView

自定义Xamarin.Forms布局

Xamarin Forms自定义步进器

Xamarin 自定义 MarkupExtension 不适用于 Type 属性

Xamarin Android Webview 文本选择不适用于自定义上下文菜单

xamarin.forms中我的自定义控件的HeightRequest的自定义BindableProperty

如何将对象作为可绑定属性传递给Xamarin.Forms自定义控件

Xamarin绑定不适用于ListView

Xamarin.Forms DependencyService不适用于所有平台

Xamarin Forms:ios:Page.UseSafeArea 不适用于 CarouselPage 中的所有孩子