gpt4 book ai didi

c# - 绑定(bind)到 WPF 中的数组

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:12 25 4
gpt4 key购买 nike

我正在开发 WPF 应用程序。出于某种原因,我的 View 模型中的值没有显示

MyViewModel.cs

public class MyViewModel : ViewModel
{
private ObservableCollection<Item> items = Item.GetAll();
public ObservableCollection<Item> Items
{
get { return items; }
set { items = value; }
}
}

public class Item
{
public int ID { get; set; }

public List<int> Tally = new List<int>();

public int Total { get; set; }

public Item(int id)
{
this.ID = id;
for (int i = 0; i < 7; i++)
this.Tally.Add(0);
}

public static ObservableCollection<Item> GetAll()
{
ObservableCollection<Item> items = new ObservableCollection<Item>();
for (int i = 0; i <= 10; i++)
{
items.Add(new Item(i));
}
return items;
}
}

MyPage.xaml

<telerik:RadGridView x:Name="myGridView" Grid.Row="1" AutoGenerateColumns="False" 
ItemsSource="{Binding Path=Items}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="ID" DataMemberBinding="{Binding Path=ID}" IsReadOnly="True" />
<telerik:GridViewDataColumn Header="Monday" DataMemberBinding="{Binding Path=Tally[0], Mode=OneWay}" IsReadOnly="True" Width="1*" />
<telerik:GridViewDataColumn Header="Tuesday" DataMemberBinding="{Binding Path=Tally[1], Mode=OneWay}" IsReadOnly="True" Width="1*" />
<telerik:GridViewDataColumn Header="Wednesday" DataMemberBinding="{Binding Path=Tally[2], Mode=OneWay}" IsReadOnly="True" Width="1*" />
<telerik:GridViewDataColumn Header="Thursday" DataMemberBinding="{Binding Path=Tally[3], Mode=OneWay}" IsReadOnly="True" Width="1*" />
<telerik:GridViewDataColumn Header="Friday" DataMemberBinding="{Binding Path=Tally[4], Mode=OneWay}" IsReadOnly="True" Width="1*" />
<telerik:GridViewDataColumn Header="Total" DataMemberBinding="{Binding Path=Total, Mode=OneWay}" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>

每个项目的条目都会按预期出现在网格中。但是,只有第一列和最后一列有值。 Monday-Friday 列(引用 Tally List 的列)中的值没有出现。我不确定我做错了什么。有人可以告诉我吗?

谢谢!

最佳答案

Tally 是一个 field , 需要公开 property .

(我也希望您知道 INPC,如果您想进行运行时更改)

关于c# - 绑定(bind)到 WPF 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8363055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com