绑定到WPF中的结构数组 [英] Binding to an array of structs in WPF
问题描述
在我的应用程序中,我存储了一个结构体数组,其中包含我需要支付给客户的票据和硬币的数量以及有效支付的数量。
In my application I store an array of structs containing how much bills/coins i need to pay to a client and how many I effectively pay.
public struct MoneyType
{ //contains more, but left out for readability
public int Requested { get; set; } // How many are requested for a payment
public int Paid { get; set; } //bills paid to the customer
}
我用这些数组制作了一个数组通过以下方式使视图模型可访问:
I made an array with these which I make accessible to the viewmodel in the following way:
MoneyType[] money; //correctly initialised in the constructor
public MoneyType[] GetMoney()
{
return money;
}
在视图模型本身中,我可以通过以下方式访问它们:
In the viewmodel itself I make these accessible in this way:
public MoneyType[] MoneyTypes //Denominations
{
get
{
return _dataService.GetMoney();
}
}
最后,在XAML中,我以以下方式访问这些文件:
Finally, in my XAML, I accessed these as:
<cbtn:BillCounter x:Name="euro200" Value = "{Binding MoneyTypes[2].Paid, Mode=TwoWay }" />
[2]用于指示我要使用哪种类型的钞票/硬币。
The [2] is used to indicate which type of bills/coins I want to use.
我想使这项工作有2种方式,以便我的自定义控件可以更新已支付的硬币/票据的数量。为此,我需要为我的MoneyTypes属性设置一个setter。
I want to make this work 2 ways, so that my custom control can update the amount of coins/bills paid. For which I would need a setter for my MoneyTypes property.
我不确定执行此操作的正确方法。我有一种感觉,我不应该通过我的viewmodel / model传递整个数组,而应该传递我的MoneyTypes之一的特定部分(付费/请求字段,带有某种指示使用哪个条目的索引)
I'm not sure the right way to do this. I have a feeling that somehow, I should not pass the entire array from my viewmodel/model, but a specific part of one of my MoneyTypes (the paid/requested fields, with some kind of index indicating which entry to use)
我不确定该怎么做。
推荐答案
可以可以帮助您- https://stackoverflow.com/a/7714924/3207043
基于此,您需要使用引用数据类型进行绑定。
Based on this, you need to use reference data types to bindings.
这篇关于绑定到WPF中的结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!