Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# 将DataGridTextColumn或DataGridTemplateColumn绑定到外键_C#_Wpf - Fatal编程技术网

C# 将DataGridTextColumn或DataGridTemplateColumn绑定到外键

C# 将DataGridTextColumn或DataGridTemplateColumn绑定到外键,c#,wpf,C#,Wpf,我对组合框问了一个类似的问题,得到了一个很好的答案。现在,如果可能的话,我想将DataGridTextColumn绑定到外键。否则,我可以将DataGridTemplateColumn绑定到外键 为了说明我的问题,让我们看一个例子: 假设我有两张桌子: 卡斯托默 CustomerID Name Gender //ForeignKey 性别 GenderID Value 我正在使用实体框架自动生成我的模型 现在,我尝试按如下方式绑定DataGridTemplateColumn: <Dat

我对组合框问了一个类似的问题,得到了一个很好的答案。现在,如果可能的话,我想将
DataGridTextColumn
绑定到
外键。否则,我可以将
DataGridTemplateColumn
绑定到
外键

为了说明我的问题,让我们看一个例子:

假设我有两张桌子:

卡斯托默

CustomerID
Name
Gender //ForeignKey
性别

GenderID
Value
我正在使用
实体框架
自动生成我的
模型

现在,我尝试按如下方式绑定DataGridTemplateColumn:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding DataContext.Customers, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
          SelectedItem="{Binding SelectedPatient}" RowDetailsVisibilityMode="VisibleWhenSelected"
          IsReadOnly="True" SelectionMode="Single" SelectionUnit="FullRow" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" Header="Patient Name" Width="25*" />
        <!--<DataGridTextColumn Binding="{Binding Gender}" Header="Gender" Width="10*" />-->
        <DataGridTemplateColumn Header="Gender" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ItemsControl ItemsSource="{Binding ???}" >
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding ???}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid>

但是我不知道正确的绑定


希望有人能帮忙。

{Binding Gender.Value}

为你的模特

public class YourViewModel
{
    public List<Customer> Customers { get; set; }
    public Customer SelectedCustomer { get; set; }
}

public class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
    public Gender Gender { get; set; }
}

public class Gender
{
    public int GenderID { get; set; }
    public string Value { get; set; }
}
公共类YourViewModel
{
公共列表客户{get;set;}
公共客户选择客户{get;set;}
}
公共类客户
{
public int CustomerID{get;set;}
公共字符串名称{get;set;}
公共性别{get;set;}
}
公共阶级性别
{
public int GenderID{get;set;}
公共字符串值{get;set;}
}

顺便说一句,性别标准化表太多了。您可以考虑使用

谢谢,这是一个非常简单的解决方案。对于将来有同样问题的用户,请更改<代码>患者< /代码>和<代码> SelectedPatients < /代码>到<代码>客户> />代码> <代码> SelectedCustomer < /代码>。