WPF Datagrid绑定 [英] WPF Datagrid Binding
问题描述
你好
我在xaml中有以下代码:
< DataGrid AutoGenerateColumns =" HorizontalGridLinesBrush =" IsReadOnly
span> =" {Binding AccessList,Mode = TwoWay}" 保证金 =" 名称 dgAccess" < DataGrid.Columns >
< DataGridTemplateColumn =" Group" < DataGridTemplateColumn.CellTemplate >
< DataTemplate >
< 组合框
SelectedItem ="
span> ItemsSource ="
span> IsReadOnly =" 背景 白色" / >
< /DataTemplate >
< /DataGridTemplateColumn.CellTemplate >
< /DataGridTemplateColumn >
< /DataGrid.Columns
网格具有对象列表(List< doctypeaccess>).
数据网格中的组合框具有对象列表(List< usergroup>).
当我连续选择一个组时,绑定将在我的对象AccessList中设置属性UserGroup.但这是行不通的. AccessList.UserGroup始终为null吗?
类DocTypeAccess
public 类DocTypeAccess
{
公共 UserGroup UserGroup {get; 设置; }
}
ViewModel上的绑定属性
public List< DocTypeAccess>存取清单
{
获取 {返回 m_listAccess; }
设置
{
m_listAccess = 值;
OnPropertyChanged(" );
}
}
是什么让我错了?
感谢您的帮助...
您没有提供UserGroup类的属性,但是如果要绑定SelectedItem,则需要标识一个每个用户组唯一的UserGroup类中的Property属性,然后需要将SelectedValue属性和SelectedValuePath绑定到此属性.因此,只需说您的UserGroup类具有一个名为UserGroupID的属性,然后您就可以像这样设置SelectedValue属性
< DataTemplate > ; < 组合框 SelectedItem =" SelectedValue =" SelectedValuePath =" span> ItemsSource =" IsReadOnly =" 背景 白色" / > < /DataTemplate >
我希望这是有道理的.如果没有,请发布您的UserGroup类代码,我们可以在那里进行工作.
顺便说一句,这假定您在ViewModel中具有一个属性,该属性返回名为SelectedDocAccessType
的DocAccessType类的实例,该实例绑定到DataGrid的SelectedItem
属性.[/Edit]
Hello
I have following code in xaml:
<DataGrid AutoGenerateColumns="False" HorizontalGridLinesBrush="Black" IsReadOnly="False"
ItemsSource="{Binding AccessList, Mode=TwoWay}" Margin="5,8,5,0" Name="dgAccess">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Group">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
SelectedItem="{Binding Path=UserGroup, Mode=TwoWay}"
ItemsSource="{Binding Path=DataContext.UserGroupList,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
IsReadOnly="False" Background="White" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns
The grid has a list of objects (List<doctypeaccess>).
The ComboBox in the datagrid has a list of objects (List<usergroup>).
When i select a group in a row, the binding sould set the property UserGroup in my object AccessList. But it not works. The AccessList.UserGroup is always null?
Class DocTypeAccess
public class DocTypeAccess
{
public UserGroup UserGroup { get; set; }
}
Binding Property on ViewModel
public List<DocTypeAccess> AccessList
{
get { return m_listAccess; }
set
{
m_listAccess = value;
OnPropertyChanged("AccessList");
}
}
What make i wrong?
Thanks for your help...
You don''t give the properties for your UserGroup class, but if you want to bind the SelectedItem you need to identify a property in the UserGroup class that is unique to each UserGroup and then you need to bind the SelectedValue property and the SelectedValuePath to this property. So, just say your UserGroup class had a property called UserGroupID then you would set the SelectedValue property like this
<DataTemplate> <ComboBox SelectedItem="{Binding Path=UserGroup, Mode=TwoWay}" SelectedValue="{Binding SelectedDocTypeAcces.UserGroup.UserGroupID}" SelectedValuePath="UserGroupID" ItemsSource="{Binding Path=DataContext.UserGroupList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" IsReadOnly="False" Background="White" /> </DataTemplate>
I hope this makes sense. If not please post your UserGroup class code and We can work from there.
[Edit] By the way, this assumes you have a property in your ViewModel that returns an instance of a DocAccessType class calledSelectedDocAccessType
, which is bound to your DataGrid''sSelectedItem
property.[/Edit]
这篇关于WPF Datagrid绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!