[英]Xamarin and MVVM - How can I bind a Boxview in my View to an array of Color in my ViewModel?
I have a two-dimensional array of Xamarin.Forms.Color objects in my ViewModel. 我的ViewModel中有一个Xamarin.Forms.Color对象的二维数组。 I would like to bind this array to a bunch of BoxView objects in my View and set their color based on the array's contents. 我想将此数组绑定到View中的一堆BoxView对象上,并根据数组的内容设置它们的颜色。 I know how to bind to properties like string, int or bool on a ViewModel, but if the specific value I want to bind to is a system-provided object like Color, I have no idea. 我知道如何绑定到ViewModel上的字符串,int或bool之类的属性,但是如果我要绑定的特定值是系统提供的对象(如Color),我也不知道。
Code snippets: 代码段:
ViewModel has this property that I would like to bind to: ViewModel具有此属性,我想将其绑定到:
public GameGrid<Color> Board;
View is not declared in XAML but instead via C# code. 视图不是在XAML中声明的,而是通过C#代码声明的。 The property I'd like bound is the ColorProperty of a BoxView, like this: 我想绑定的属性是BoxView的ColorProperty,如下所示:
boxView.BindingContext = _viewModel.Board[row, col];
boxView.SetBinding(BoxView.ColorProperty, ".", BindingMode.Default);
Given that the _viewModel.Board property is a cell in a two-dimentional array or type Color, how do I bind to it? 鉴于_viewModel.Board属性是二维数组中的单元格或类型为Color,如何绑定到它? The "." “。” is a placeholder - I don't know what I should place there. 是一个占位符-我不知道该放置什么。
The GameGrid class wraps a two-dimensional array, because I figured I would need to implement INotifyPropertyChanged so I can react to individual element changes in this array later, in order to animate a game move. GameGrid类包装了一个二维数组,因为我认为我需要实现INotifyPropertyChanged,以便稍后可以对该数组中的各个元素更改做出反应,以便为游戏移动制作动画。 For completeness, its code is here: 为了完整起见,其代码如下:
public class GameGrid<T> : INotifyPropertyChanged
{
private T[,] _array;
public GameGrid(int rows, int columns)
{
_array = new T[rows, columns];
}
public T this[int a, int b]
{
get
{
return _array[a, b];
}
set
{
_array[a, b] = value;
RaisePropertyChanged(nameof(GameGrid<T>));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(
this, new PropertyChangedEventArgs(propertyName));
}
}
If I change my two-dimensional array to be a simple object, eg; 如果我将二维数组更改为简单对象,例如;
public class Wrapper
{
public Color BindThis { get; set; }
}
public GameGrid<Wrapper> Board;
it is pretty trivial to bind to that property like this: 像这样绑定到该属性非常简单:
boxView.SetBinding(BoxView.ColorProperty, "BindThis", BindingMode.Default);
but that seems needlessly complicated. 但这似乎不必要地复杂。
It looks like you're on the right track. 看来您在正确的轨道上。 A single dot ( .
) is the syntax to bind to the BindingContext
itself; 单个点( .
)是绑定到BindingContext
本身的语法。 not a property on the BindingContext
, but the object that is the current BindingContext
. 不是BindingContext
的属性,而是当前BindingContext
的对象。
Have you tried just running the code with the .
您是否尝试过使用来运行代码.
as the Binding
expression? 作为Binding
表达式?
BoxView.Color
is of type Xamarin.Forms.Color
, so you should be able to bind to that directly. BoxView.Color
的类型为Xamarin.Forms.Color
,因此您应该能够直接绑定到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.