我的观点有什么不对? Xamdatagrid 不显示组合框数据
What's not right in my View ? Xamdatagrid is not displaying the combo box data
我想在 MVVM 的 xamDataGrid 中显示 products.xml 中的数据 style.Binding 无法正常工作。 Product 节点中的值必须添加到 xamDatagrid 中的组合框字段。组合框未显示任何数据。有人可以教我如何解决这个问题吗?
Product.xml
<?xml version="1.0" encoding="utf-8" ?>
<WG>
<WGT>
<TName>tanme1</TName>
<Fname>fname1</Fname>
<Product>Product1</Product>
<Product>Product2 </Product>
</WGT>
<WGT>
<TName>tanme2</TName>
<Fname>fname2</Fname>
</WGT>
<WGT>
<TName>tanme1</TName>
<Fname>fname1</Fname>
<Product>Product1</Product>
<Product>Product2 </Product>
<Product>Product3 </Product>
<Product>Product4 </Product>
</WGT>
<WGT>
<TName>tanme5</TName>
<Fname>fname5</Fname>
</WGT>
<WGT>
<TName>tanme1</TName>
<Fname>fname1</Fname>
<Product>Product1</Product>
<Product>Product2 </Product>
<Product>Product3 </Product>
<Product>Product4 </Product>
<Product>Product5 </Product>
<Product>Product6 </Product>
</WGT>
</WG>
products.cs [viewModel Class]
namespace XmlTest.ViewModel
{
public partial class WG : ViewBase
{
private WGWGT[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("WGT", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public WGWGT[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
public void display()
{
XmlSerializer deserializer = new XmlSerializer(typeof(WG));
TextReader reader = new StreamReader(@"C:\xlf\products.xml");
object obj = deserializer.Deserialize(reader);
WG XmlData = (WG)obj;
itemsField = XmlData.Items;
}
}
public partial class WGWGT : ViewBase
{
private string tNameField;
private string fnameField;
private WGWGTProduct[] productField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TName
{
get
{
return this.tNameField;
}
set
{
this.tNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Fname
{
get
{
return this.fnameField;
}
set
{
this.fnameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Product", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true)]
public WGWGTProduct[] Product
{
get
{
return this.productField;
}
set
{
this.productField = value;
}
}
}
public partial class WGWGTProduct : ViewBase
{
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
}
View.xml
<Grid>
<igDP:XamDataGrid x:Name="dataGridView1" DataSource="{Binding Items}"
HorizontalAlignment="Left" Height="800" Margin="10,56,0,0" VerticalAlignment="Top" Width="800">
<igDP:XamDataGrid.FieldLayoutSettings >
<igDP:FieldLayoutSettings AutoGenerateFields="False"/>
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldLayouts >
<igDP:FieldLayout>
<igDP:FieldLayout.Fields >
<igDP:Field Label="Template Name" Name="TName" />
<igDP:Field Label="File Name" Name="Fname"/>
<igDP:UnboundField Label="Product Name" >
<igDP:UnboundField.Settings>
<igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}" AllowEdit="True">
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igEditors:XamComboEditor}">
<Setter Property="ItemsSource" Value="{Binding Product}" />
<Setter Property="DisplayMemberPath" Value="Value" />
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:UnboundField.Settings>
</igDP:UnboundField>
</igDP:FieldLayout.Fields>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</Grid>
View.xaml.cs
public partial class View : Window
{
public View()
{
InitializeComponent();
WaferGen wg = new WaferGen();
this.DataContext = wg;
wg.display();
}
}
我终于想出了解决办法。我将它张贴在这里,以便它可以帮助像我这样的人。
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:CellValuePresenter}}, Path=Record.DataItem.Product}" />
<Setter Property="DisplayMemberPath" Value="Value" />
我想在 MVVM 的 xamDataGrid 中显示 products.xml 中的数据 style.Binding 无法正常工作。 Product 节点中的值必须添加到 xamDatagrid 中的组合框字段。组合框未显示任何数据。有人可以教我如何解决这个问题吗?
Product.xml
<?xml version="1.0" encoding="utf-8" ?>
<WG>
<WGT>
<TName>tanme1</TName>
<Fname>fname1</Fname>
<Product>Product1</Product>
<Product>Product2 </Product>
</WGT>
<WGT>
<TName>tanme2</TName>
<Fname>fname2</Fname>
</WGT>
<WGT>
<TName>tanme1</TName>
<Fname>fname1</Fname>
<Product>Product1</Product>
<Product>Product2 </Product>
<Product>Product3 </Product>
<Product>Product4 </Product>
</WGT>
<WGT>
<TName>tanme5</TName>
<Fname>fname5</Fname>
</WGT>
<WGT>
<TName>tanme1</TName>
<Fname>fname1</Fname>
<Product>Product1</Product>
<Product>Product2 </Product>
<Product>Product3 </Product>
<Product>Product4 </Product>
<Product>Product5 </Product>
<Product>Product6 </Product>
</WGT>
</WG>
products.cs [viewModel Class]
namespace XmlTest.ViewModel
{
public partial class WG : ViewBase
{
private WGWGT[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("WGT", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public WGWGT[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
public void display()
{
XmlSerializer deserializer = new XmlSerializer(typeof(WG));
TextReader reader = new StreamReader(@"C:\xlf\products.xml");
object obj = deserializer.Deserialize(reader);
WG XmlData = (WG)obj;
itemsField = XmlData.Items;
}
}
public partial class WGWGT : ViewBase
{
private string tNameField;
private string fnameField;
private WGWGTProduct[] productField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TName
{
get
{
return this.tNameField;
}
set
{
this.tNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Fname
{
get
{
return this.fnameField;
}
set
{
this.fnameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Product", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = true)]
public WGWGTProduct[] Product
{
get
{
return this.productField;
}
set
{
this.productField = value;
}
}
}
public partial class WGWGTProduct : ViewBase
{
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
}
View.xml
<Grid>
<igDP:XamDataGrid x:Name="dataGridView1" DataSource="{Binding Items}"
HorizontalAlignment="Left" Height="800" Margin="10,56,0,0" VerticalAlignment="Top" Width="800">
<igDP:XamDataGrid.FieldLayoutSettings >
<igDP:FieldLayoutSettings AutoGenerateFields="False"/>
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldLayouts >
<igDP:FieldLayout>
<igDP:FieldLayout.Fields >
<igDP:Field Label="Template Name" Name="TName" />
<igDP:Field Label="File Name" Name="Fname"/>
<igDP:UnboundField Label="Product Name" >
<igDP:UnboundField.Settings>
<igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}" AllowEdit="True">
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igEditors:XamComboEditor}">
<Setter Property="ItemsSource" Value="{Binding Product}" />
<Setter Property="DisplayMemberPath" Value="Value" />
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:UnboundField.Settings>
</igDP:UnboundField>
</igDP:FieldLayout.Fields>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</Grid>
View.xaml.cs
public partial class View : Window
{
public View()
{
InitializeComponent();
WaferGen wg = new WaferGen();
this.DataContext = wg;
wg.display();
}
}
我终于想出了解决办法。我将它张贴在这里,以便它可以帮助像我这样的人。
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type igDP:CellValuePresenter}}, Path=Record.DataItem.Product}" />
<Setter Property="DisplayMemberPath" Value="Value" />