我正在尝试在WPF数据网格中包含一个ComboBox列。我正在尝试将此列绑定到ViewModel中的一个可观察集合,但是,在运行时,单元格仍为空。Datacontext是正确的,因为所有普通列都成功绑定。我想在UI中显示'regionShortCode‘。下面是我的xaml:
<DataGridComboBoxColumn Header="Region" DisplayMemberPath="regionShortCode" Width="SizeToHeader">
<DataGridComboBoxColumn.ElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding Path=MembershipsCollection}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding Path=MembershipsCollection}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>这是我在ViewModel中声明的ObservableCollection。从构造函数中调用的方法成功填充Collection:
private ObservableCollection<tbAccountMembership> _MembershipsCollection;
public ObservableCollection<tbAccountMembership> MembershipsCollection
{
get { return _MembershipsCollection; }
set
{
_MembershipsCollection = value;
OnPropertyChanged("MembershipsCollection");
}
} 在运行时,我得到:
System.Windows.Data Error: 40 : BindingExpression path error: 'MembershipsCollection' property not found on 'object' ''tbAccountMembership_041E43AFC29975F12C156BA1373ACD47FC07BBE55614E5AF8AD3BBD9F090C133' (HashCode=46247020)'. BindingExpression:Path=MembershipsCollection; DataItem='tbAccountMembership_041E43AFC29975F12C156BA1373ACD47FC07BBE55614E5AF8AD3BBD9F090C133' (HashCode=46247020); target element is 'TextBlockComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')在“输出”窗口中。为什么Xaml不能解析这个绑定?谢谢
发布于 2014-09-03 20:26:24
如果您希望从DataGrid数据绑定到视图模型中的单个集合属性,那么您的答案是为什么Xaml不能解析此绑定?是因为您没有告诉Framework在哪里查找实际的属性...您将需要使用[RelativeSource Binding]1。试着这样做:
<DataGridComboBoxColumn Header="Region" DisplayMemberPath="regionShortCode"
Width="SizeToHeader">
<DataGridComboBoxColumn.ElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding
DataContext.MembershipsCollection, RelativeSource={RelativeSource
AncestorType={x:Type YourViewModelsPrefix:YourViewModel}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style>
<Setter Property="ComboBox.ItemsSource" Value="{Binding
DataContext.MembershipsCollection, RelativeSource={RelativeSource
AncestorType={x:Type YourViewsPrefix:YourView}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>使用此Binding Path,框架将在常规DataGrid.DataContext之外查找对象中名为MembershipsCollection的属性,该属性被设置为YourView的DataContext (显然这不是实际名称) UserControl或Window。如果您的视图模型被正确设置为DataContext,那么这应该可以很好地工作。
发布于 2015-05-14 18:25:32
您必须提供集合的确切路径,以便与combobox的itemssource绑定。为此,将您的祖先设置为您的主要控件,即窗口或用户控件
<DataGridComboBoxColumn Header="Category"
SelectedValueBinding="{Binding category_cde}"
SelectedValuePath="category_cde"
DisplayMemberPath="category_dsc">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=CATEGORIES , RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Path=CATEGORIES , RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>下面是我在代码中定义的类别集合,其中类别是您可以定义自己的集合的某个类
List<Category> m_categories = new List<Category>();
public List<Category> CATEGORIES
{
get { return m_categories; }
set { m_categories = value; }
}发布于 2015-09-16 20:48:20
使用DataGridComboBoxColumn的一个更简单的解决方案是在窗口类的构造函数中以编程方式设置ItemSource绑定。
<DataGrid x:Name="MembershipGridNormal" AutoGenerateColumns="False" SelectedIndex="0" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Prop1}"/>
<DataGridTextColumn Header="Value" Binding="{Binding Prop2}"/>
<DataGridComboBoxColumn x:Name="CmbMemberShips" Header="RawValues" DisplayMemberPath="Name" />
</DataGrid.Columns>
</DataGrid>在后台代码中
public Win32599087()
{
InitializeComponent();
MemberShipGridNormal.DataContext = myCollection;
Binding b = new Binding();
b.Source = myCollection;
b.Path = new PropertyPath("collectionpropertyname");
BindingOperations.SetBinding(CmbMemberships, DataGridComboBoxColumn.ItemsSourceProperty, b);
}https://stackoverflow.com/questions/25644003
复制相似问题