wpf datagridcombobox linq外键

时间:2017-01-09 18:58:35

标签: wpf linq combobox

我已经搜遍了所有并且继续寻找不适合我的类似例子。有人可以指出我搞砸了吗?

我有两个表AlarmName和AlarmLevel。由外键AlarmName.AlarmLevelID链接 - > AlarmLevel.Id。

我的XAML中有以下内容:

                <DataGrid Name="Alarms"  AutoGenerateColumns="false">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="75" />
                    <DataGridTextColumn Binding="{Binding Description}" Header="Description" />
                    <DataGridTextColumn Binding="{Binding AlarmLevel.Name}" Header="AName" Width="75"/>

                    <DataGridTemplateColumn Header="Alarm Level">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding DataContext.AlarmLevel, RelativeSource={RelativeSource  AncestorType={x:Type DataGrid}}}"
                                          DisplayMemberPath="Name"  
                                          SelectedValuePath="AlarmLevelID"
                                          SelectedValue="{Binding Id}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>

            </DataGrid>

以下代码背后是:

UISimulation1.Data.UISimulationDBDataContext db = new UISimulation1.Data.UISimulationDBDataContext();

var alarm = (from v in db.AlarmNames
            select v);

Alarms.ItemsSource = alarm;

DataGridTextColumns工作并提取正确的数据。但是,无论我尝试什么,我都无法使组合框工作或显示任何内容。

显然,我错过了一些明显的东西;但是已经尝试了50种基于google示例的组合框变体,结果为零。

- 室温

2 个答案:

答案 0 :(得分:0)

您需要使用将ComboBox的ItemsSource属性绑定到的公共属性公开AlarmLevel表中的记录。请参阅以下示例代码。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        using (UISimulation1.Data.UISimulationDBDataContext db = new UISimulation1.Data.UISimulationDBDataContext())
        {
            AlarmNames = (from v in db.AlarmNames
                          select v).ToList();

            AlarmLevels = (from v in db.AlarmLevels
                           select v).ToList();
        }
    }

    public IEnumerable AlarmNames { get; private set; }

    public IEnumerable AlarmLevels { get; private set; }
}
<DataGrid Name="Alarms" ItemsSource="{Binding AlarmNames}" AutoGenerateColumns="false">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" Header="Name" Width="75" />
        <DataGridTextColumn Binding="{Binding Description}" Header="Description" />
        <DataGridTextColumn Binding="{Binding AlarmLevel.Name}" Header="AName" Width="75"/>
        <DataGridTemplateColumn Header="Alarm Level">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding DataContext.AlarmLevels, RelativeSource={RelativeSource  AncestorType={x:Type DataGrid}}}"
                                          DisplayMemberPath="Name"  
                                          SelectedValuePath="AlarmLevelID"
                                          SelectedValue="{Binding Id}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

答案 1 :(得分:0)

DataGrid列不属于DataGrid的可视或逻辑树,请参阅this