[英]How I can Binding c# class with BindingList inside?
I have this c# class with BindingList 我有这个带有BindingList的c#类
public class Jednotka
{
// ~ctors
public Jednotka()
{
CisloJednotky = "";
PodilNaSVJCitatel = 0;
PodilNaSVJJmenovatel = 1;
TypJednotky = "";
VlastniciJednotky = new BindingList<Vlastnik>();
}
public string CisloJednotky { get; set; }
public int PodilNaSVJCitatel { get; set; }
public int PodilNaSVJJmenovatel { get; set; }
public string TypJednotky { get; set; }
public BindingList<Vlastnik> VlastniciJednotky { get; set; }
}
and Class Vlastnik 和Vlastnik级
public class Vlastnik : Osoba
{
public Vlastnik()
{
this.PodilNaJednotceCitatel = 0;
this.PodilNaJednotceJmenovatel = 1;
this.Bydliste = "";
this.Jmeno = "";
}
public int PodilNaJednotceCitatel { get; set; }
public int PodilNaJednotceJmenovatel { get; set; }
}
I need bind to DataGrid only "CisloJednotky" with the right "Jmeno". 我需要使用正确的“Jmeno”绑定到DataGrid“CisloJednotky”。
In my program is a lot of "Jednotka" and each one may have one or more "Jmeno". 在我的程序中有很多“Jednotka”,每个人可能有一个或多个“Jmeno”。
How I can show this two properties in DataGrid. 我如何在DataGrid中显示这两个属性。
Can I use xaml Binding ? 我可以使用xaml Binding吗?
I'll try this send to xaml 我会尝试将此发送到xaml
DataGridUcast.ItemsSource = SvJschuze.Jednotky;
and in xaml 并在xaml
<DataGrid Name="DataGridUcast" Grid.Row="1"
AutoGenerateColumns="False"
ItemsSource="{Binding Jednotky/VlastniciJednotky}">
<!--Height="{Binding ActualHeight, RelativeSource ={RelativeSource AncestorType = {x:Type Window}}}"-->
<DataGrid.Columns>
<DataGridTextColumn Header="Číslo" Binding="{Binding CisloJednotky}" MinWidth="100"/>
<DataGridTextColumn Header="Jméno" Binding="{Binding Path=Vlastnik.Jmeno}" MinWidth="330" Width="Auto"/>
<!--<DataGridTextColumn Header="Účast" MinWidth="50"/>-->
<DataGridCheckBoxColumn Header="Účast" Binding="{Binding Pritomen, XPath=Pritomen}" MinWidth="50"/>
</DataGrid.Columns>
</DataGrid>
I'll tried to do more ways, but no success. 我会尝试做更多的方法,但没有成功。 I have only "CisloJednotky" inside DataGrid.
我在DataGrid中只有“CisloJednotky”。 But only one number although "CisloJednotky" have more "Vlastnik" with "Jmeno".
但只有一个数字虽然“CisloJednotky”有更多“Vlastnik”与“Jmeno”。
If don't underestand my code. 如果不是我的代码和我的代码。 I can send it again in english.
我可以用英语再发一次。
Thanks a lot for any tips. 非常感谢任何提示。
I have a solution. 我有一个解决方案。 Use helping class.
使用帮助课程。
public class Data
{
// ~ctors
public Data()
{
Jmeno = "";
Cislo = "";
Pritomen = false;
}
public BindingList<Data> NaplnData(SVJschuze svJschuze, BindingList<Data> dataList)
{
foreach (var jednotka in svJschuze.Jednotky)
{
foreach (var vlastnik in jednotka.VlastniciJednotky)
{
Data data = new Data();
data.Cislo = jednotka.CisloJednotky;
data.Jmeno = vlastnik.Jmeno;
dataList.Add(data);
}
}
return dataList;
}
// Fields and properties
public string Jmeno { get; set; }
public string Cislo { get; set; }
public bool Pritomen { get; set; }
}
and fill help class. 并填写帮助类。
Data data = new Data();
DataList = new BindingList<Data>();
data.NaplnData(SvJschuze, DataList);
DataGridUcast.ItemsSource = DataList;
Work good for me. 对我有益。 Better be use DataGridComboBoxColumn in xaml for Vlastnik class.
最好在xaml中使用DataGridComboBoxColumn来获取Vlastnik类。 Can choose the right name for Jednotka.
可以为Jednotka选择正确的名称。 But I don't know how.
但我不知道怎么做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.