Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在DataGridComboxColumn中绑定相关数据_C#_Wpf_Datagrid_Datagridcomboboxcolumn - Fatal编程技术网

C# 在DataGridComboxColumn中绑定相关数据

C# 在DataGridComboxColumn中绑定相关数据,c#,wpf,datagrid,datagridcomboboxcolumn,C#,Wpf,Datagrid,Datagridcomboboxcolumn,我有一个datagrid,可以很好地显示数据,但现在我想用它来编辑数据。如何配置一个DataGridComboxColumn来显示描述,但绑定到相关表的ID 我的数据通过EF进入应用程序,但为了将正确过滤的数据放入数据网格,我使用DbQuery创建列表,然后将其设置为数据网格的项目资源: private void OnControlLoaded(object sender, RoutedEventArgs e) { DbQuery<Product> whiteGoodsProdu

我有一个datagrid,可以很好地显示数据,但现在我想用它来编辑数据。如何配置一个
DataGridComboxColumn
来显示描述,但绑定到相关表的ID

我的数据通过EF进入应用程序,但为了将正确过滤的数据放入
数据网格
,我使用
DbQuery
创建
列表
,然后将其设置为
数据网格
项目资源

private void OnControlLoaded(object sender, RoutedEventArgs e)
{
  DbQuery<Product> whiteGoodsProductQuery = this.GetWhitegoodsProductsQuery(myEntities);
  List<Product> lstWhiteGoodsProducts = whiteGoodsProductQuery.ToList<Product>();
  dgridWhitegoodProducts.ItemsSource = lstWhiteGoodsProducts;
}
现在,我想将制造商
DataGridTextColumn
更改为
datagridcomboxcolumn
,以便用户可以选择其他制造商。当我设置ComboBox列时,我没有得到任何数据,所以我显然在绑定中做了一些错误的事情

下面是我的组合框列最初的样子:

<DataGridComboBoxColumn Header="Manufacturer"
    SelectedValueBinding="{Binding ManufacturerID, Mode=TwoWay}" 
    SelectedValuePath="Manufacturer.ID" 
    DisplayMemberPath="Manufacturer.CompanyName" />

目前看来:

<DataGridComboBoxColumn Header="Manufacturer" SelectedValueBinding="{Binding 
    ManufacturerID}" SelectedValuePath="ID" DisplayMemberPath="CompanyName}" 
    ItemsSource="{Binding Source={StaticResource ManufacturerList}" />

ItemsSource来自代码隐藏中的静态ObservableCollection

public static ObservableCollection<Manufacturer> ManufacturerList = new 
    ObservableCollection<Manufacturer>(new MyEntities().Manufacturers);
public static observeCollection ManufacturerList=new
可观察到的收集(新MyEntities().制造商);
我尝试了SelectedValuePath、SelectedValueBinding等属性中[TableName.]字段的多种变体,但均无效

接下来,我尝试在代码隐藏中创建ComboBoxColumn可以绑定到的列表,但当我将列表设置为ItemsSource时,我得到:
System.Windows.Data错误:2:找不到目标元素的治理FrameworkElement或FrameworkContentElement。BindingExpression:Path=ManufacturerList;DataItem=null;目标元素是“DataGridComboxColumn”(HashCode=14427695);目标属性为“ItemsSource”(类型为“IEnumerable”)

将该列表更改为ObservableCollection也不起作用,使其成为静态也不起作用


有人能看出我做错了什么,以及我应该走哪条路吗?

您必须定义一个用户控制资源,如bellow

<UserControl.Resources>
    <CollectionViewSource x:Key="UserTypeList" Source="{Binding DomainUserTypes}" /> </UserControl.Resources>

  • 绑定数据网格组合列,如下所示

    DataGridComboxColumn标题=“用户类型” ItemsSource=“{Binding Source={StaticResource UserTypeList}”

  • DomainType将是视图模型属性

    DomainUserTypes=新的ObservableCollection()


  • 只需使用数据填充视图模型属性并进行测试,它就可以工作。

    您只能绑定到公共属性,而
    ManufacturerList
    是一个字段

    如果将其定义为
    UserControl
    的代码隐藏中的非静态属性:

    public ObservableCollection<Manufacturer> ManufacturerList { get; } = new ObservableCollection<Manufacturer>(new MyEntities().Manufacturers);
    

    …其中
    ManufacturerList
    CollectionViewSource
    x:Key
    ,您在哪里设置
    DataGridComboBoxColumn
    ItemsSource
    ?主题可以帮助您。这在xaml中。我没有包括它,因为我在那里尝试了一些不同的东西。我已经尝试将ItemsSource设置为{绑定制造商},以及我在code behind中创建的列表/ObservableCollection。我通常会遇到以下错误:
    System.Windows.Data错误:2:找不到目标元素的治理FrameworkElement或FrameworkContentElement
    显示如何设置
    ItemsSource
    并在视图模型中显示设置为items源的属性。@Maxim Update.um,它不是正确的mvvm,因此查看模型==代码隐藏。
    {StaticResource Manufacturer}
    它是什么?在哪里定义了
    Manufacturer
    ?无论如何,如果在窗口的代码隐藏中正确定义了
    ManufacturerList
    ,则应该工作:
    ItemsSource=“{Binding Path=DataContext.ManufacturerList,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},Mode=OneWay}"
    我在以前的一些尝试中将列表设置为属性,然后我尝试将其设置为静态,然后我认为属性不能是静态的,并将其更改为字段。Anyhoo。我按照您的建议接收到:System.Windows.Data错误:40:BindingExpression路径错误:“在”“对象”“集合上找不到ManufacturerList”属性onViewSource'(HashCode=1121399')。BindingExpression:Path=DataContext.ManufacturerList;DataItem='ProductsTabData'(Name='ucProductsTabItem');目标元素为'TextBlockComboBox'(Name='');目标属性为'ItemsSource'(类型为'IEnumerable')如果有帮助的话,我的应用程序是一个
    窗口
    包含一个
    TabControl
    ,包含多个
    UserControl
    s(我称之为“TabItem”)。选项卡项资源包括一个包含多个
    CollectionViewSource
    s的
    ResourceDictionary
    。这就是应用程序的主要数据来源。我想知道是否应该添加另一个CollectionViewSource来捕获制造商?谢谢mm8。我没有意识到现有的数据上下文也会导致问题everywhere都说DataGridComboBoxColumn不在树中,所以我认为这部分不重要。我真的很抱歉。谢谢。解决了。
    public ObservableCollection<Manufacturer> ManufacturerList { get; } = new ObservableCollection<Manufacturer>(new MyEntities().Manufacturers);
    
    <DataGridComboBoxColumn Header="Manufacturer" SelectedValueBinding="{Binding ManufacturerID, Mode=TwoWay}" 
                            SelectedValuePath="ID" 
                            DisplayMemberPath="CompanyName">
        <DataGridComboBoxColumn.ElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding DataContext.ManufacturerList, RelativeSource={RelativeSource AncestorType=UserControl}}" />
            </Style>
        </DataGridComboBoxColumn.ElementStyle>
        <DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="ComboBox">
                <Setter Property="ItemsSource" Value="{Binding DataContext.ManufacturerList, RelativeSource={RelativeSource AncestorType=UserControl}}" />
            </Style>
        </DataGridComboBoxColumn.EditingElementStyle>
    </DataGridComboBoxColumn>
    
    <DataGridComboBoxColumn ... ItemsSource="{Binding Source={StaticResource ManufacturerList}}" />