简体   繁体   English

将外键属性绑定到DataGrid ViewSource

[英]Binding Foreign Key Properties to DataGrid ViewSource

I have this model: Model I have a datagrid that shows the component information via a collectionViewSource, and i want a ComboBoxColumn to display the component's programmer.name, as well as show all the other programmers when expanding the combobox so the user can change who is to program that component. 我有一个模型: 模型我有一个数据网格,该数据网格通过collectionViewSource显示组件信息,并且我希望ComboBoxColumn显示组件的程序员名,并在扩展组合框时显示所有其他程序员,以便用户可以更改谁是对该组件进行编程。 Here is my code: 这是我的代码:

    public partial class MainWindow : Window
{

    trackerDBEntities context = new trackerDBEntities();
    CollectionViewSource componentViewSource;

    public MainWindow()
    {
        InitializeComponent();

        componentViewSource = ((CollectionViewSource)(FindResource("componentViewSource")));
        DataContext = this;

    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        System.Windows.Data.CollectionViewSource componentViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("componentViewSource")));
        // Load data by setting the CollectionViewSource.Source property:
        context.Components.Load();
        componentViewSource.Source = context.Components.Local;

    }
}

XAML: XAML:

<Window x:Class="ToolbarDBIntegration.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ToolbarDBIntegration"
    mc:Ignorable="d"
    Title="MainWindow" Loaded="Window_Loaded">
<Window.Resources>
    <CollectionViewSource x:Key="componentViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Component}, CreateList=True}"/>
</Window.Resources>
<Grid DataContext="{StaticResource componentViewSource}">
    <DataGrid x:Name="componentDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="compFilePathColumn" Binding="{Binding CompFilePath}" Header="Comp File Path" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="compNameColumn" Binding="{Binding CompName}" Header="Comp Name" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="departmentIdColumn" Binding="{Binding DepartmentId}" Header="Department Id" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="designerColumn" Binding="{Binding Designer}" Header="Designer" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="jobIdColumn" Binding="{Binding JobId}" Header="Job Id" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="materialColumn" Binding="{Binding Material}" Header="Material" Width="SizeToHeader"/>
            <DataGridTemplateColumn x:Name="postedDateColumn" Header="Posted Date" Width="SizeToHeader">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <DatePicker SelectedDate="{Binding PostedDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn x:Name="programmerColumn" Header="Programmer" Width="SizeToHeader">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox>
                            <ComboBoxItem Content="{Binding Programmer.Name}" IsSelected="True"/>
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn x:Name="programmer_IdColumn" Binding="{Binding Programmer_Id}" Header="Programmer Id" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="qtyMirColumn" Binding="{Binding QtyMir}" Header="Qty Mir" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="qtyShnColumn" Binding="{Binding QtyShn}" Header="Qty Shn" Width="SizeToHeader"/>
            <DataGridCheckBoxColumn x:Name="statusColumn" Binding="{Binding Status}" Header="Status" Width="SizeToHeader"/>
            <DataGridTemplateColumn x:Name="timeOnlineColumn" Header="Time Online" Width="SizeToHeader">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <DatePicker SelectedDate="{Binding TimeOnline, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn x:Name="xSizeColumn" Binding="{Binding XSize}" Header="XSize" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="ySizeColumn" Binding="{Binding YSize}" Header="YSize" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="zSizeColumn" Binding="{Binding ZSize}" Header="ZSize" Width="SizeToHeader"/>
        </DataGrid.Columns>
    </DataGrid>

</Grid>

This shows the correct programmer(name) selected for each component, but there i can't figure out how to show the other programmers when expanding the combobox. 这显示了为每个组件选择的正确程序员(名称),但是在扩展组合框时我无法弄清楚如何显示其他程序员。 I have tried binding an itemsource like this: 我试图绑定这样的itemsource:

                <DataGridTemplateColumn x:Name="programmerColumn" Header="Programmer" Width="SizeToHeader">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=Programmers, Mode=TwoWay}" DisplayMemberPath="Name">
                            <ComboBoxItem Content="{Binding Programmer.Name}" IsSelected="True"/>
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

But that doesn't work. 但这是行不通的。 Do i have a problem with the way my model is configured? 我的模型配置方式是否有问题? It seems i can't access Programmers as a collection, it only recognizes the one programmer per component. 似乎我无法以集合的身份访问Programmers,它只能识别每个组件一个程序员。

I have looked at 20+ similar posts but i can't figure out what i am missing, any direction would be wonderful. 我看过20多个类似的帖子,但我无法弄清楚自己所缺少的内容,任何方向都很好。

First, you're going to need to create a separate collection view source for your Programmers collection. 首先,您将需要为Programmers集合创建一个单独的集合视图源。 Then populate it just like you do for Components . 然后像对Components一样填充它。

<CollectionViewSource x:Key="programmerViewSource"
                      d:DesignSource="{d:DesignInstance {x:Type l:Programmer}"/>
var componentViewSource = (CollectionViewSource)FindResource("componentViewSource");
var programmerViewSource = (CollectionViewSource)FindResource("programmerViewSource");

context.Components.Load();
context.Programmers.Load();

componentViewSource.Source = context.Components.Local;
programmerViewSource.Source = context.Programmers.Local;

Now, replace your column definition like so: 现在,像这样替换您的列定义:

<DataGridComboBoxColumn Header="Programmer"
                        DisplayMemberPath="Name"
                        ItemsSource="{Binding Source={StaticResource programmerViewSource}}"
                        SelectedItemBinding="{Binding Programmer}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM