0

我的问题如下:我有ContentPage一个ResourceDictionary带有一些 DataTemplates 的我。其中一个模板采用HorseObject(其中包含List<Weight>'s)。我显示了该对象的几个统计信息,但是当我想将变量从Horse对象传递到另一个视图时,如下所示:

<graph:LineGraph x:Name="displayWeight" WeightOfHorse="{Binding Weight}"/>

代码背后WeightOfHorse

public static readonly BindableProperty WeightOfHorseProperty = BindableProperty.Create(
    nameof(WeightOfHorse),
    typeof(IList<Weight>),
    typeof(LineGraph),
    defaultBindingMode: BindingMode.TwoWay);

public IList<Weight> WeightOfHorse
{
    get => (IList<Weight>)GetValue(WeightOfHorseProperty);
    set => SetValue(WeightOfHorseProperty, value);
}

我只是无法传递这些值,我知道它们是可用的,因为我可以将它们显示在一个简单的Collectionview.

我究竟做错了什么?

4

2 回答 2

1

请尝试将 IList 更改为 ObservableCollection。

于 2020-09-26T17:41:37.630 回答
1

我想通了,这行得通。看来您需要通过更改事件来分配属性。

public static readonly BindableProperty WeightOfHorseProperty = BindableProperty.Create(
nameof(WeightOfHorse),
typeof(IEnumerable<Weight>),
typeof(LineGraph),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: OnEventNameChanged);

public IEnumerable<Weight> WeightOfHorse
{
get => (IEnumerable<Weight>)GetValue(WeightOfHorseProperty);
set => SetValue(WeightOfHorseProperty, value);
}

static void OnEventNameChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (LineGraph)bindable;
control.WeightOfHorse = (List<Weight>)newValue;
...
}
于 2020-09-27T08:08:26.767 回答