首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF DataGridComboBoxColumn绑定?

WPF DataGridComboBoxColumn绑定?
EN

Stack Overflow用户
提问于 2014-09-03 20:12:38
回答 3查看 9.5K关注 0票数 1

我正在尝试在WPF数据网格中包含一个ComboBox列。我正在尝试将此列绑定到ViewModel中的一个可观察集合,但是,在运行时,单元格仍为空。Datacontext是正确的,因为所有普通列都成功绑定。我想在UI中显示'regionShortCode‘。下面是我的xaml:

代码语言:javascript
复制
   <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:

代码语言:javascript
复制
private ObservableCollection<tbAccountMembership> _MembershipsCollection;
    public ObservableCollection<tbAccountMembership> MembershipsCollection
    {
        get { return _MembershipsCollection; }
        set
        {
            _MembershipsCollection = value;
            OnPropertyChanged("MembershipsCollection");
        }
    }     

在运行时,我得到:

代码语言:javascript
复制
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不能解析这个绑定?谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-03 20:26:24

如果您希望从DataGrid数据绑定到视图模型中的单个集合属性,那么您的答案是为什么Xaml不能解析此绑定?是因为您没有告诉Framework在哪里查找实际的属性...您将需要使用[RelativeSource Binding]1。试着这样做:

代码语言:javascript
复制
<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的属性,该属性被设置为YourViewDataContext (显然这不是实际名称) UserControlWindow。如果您的视图模型被正确设置为DataContext,那么这应该可以很好地工作。

票数 1
EN

Stack Overflow用户

发布于 2015-05-14 18:25:32

您必须提供集合的确切路径,以便与combobox的itemssource绑定。为此,将您的祖先设置为您的主要控件,即窗口或用户控件

代码语言:javascript
复制
           <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>

下面是我在代码中定义的类别集合,其中类别是您可以定义自己的集合的某个类

代码语言:javascript
复制
 List<Category> m_categories = new List<Category>();
    public List<Category> CATEGORIES
    {
        get { return m_categories; }
        set { m_categories = value; }
    }
票数 1
EN

Stack Overflow用户

发布于 2015-09-16 20:48:20

使用DataGridComboBoxColumn的一个更简单的解决方案是在窗口类的构造函数中以编程方式设置ItemSource绑定。

代码语言:javascript
复制
<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>

在后台代码中

代码语言:javascript
复制
public Win32599087()
        {
            InitializeComponent();

            MemberShipGridNormal.DataContext = myCollection;

            Binding b = new Binding();
            b.Source =  myCollection;
            b.Path = new PropertyPath("collectionpropertyname");

            BindingOperations.SetBinding(CmbMemberships, DataGridComboBoxColumn.ItemsSourceProperty, b);
        }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25644003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档