Binding Foreign Key Properties to DataGrid ViewSource

时间:2018-01-23 19:31:55

标签: c# wpf binding entity

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. 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:

<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:

                <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.

I have looked at 20+ similar posts but i can't figure out what i am missing, any direction would be wonderful.

1 个答案:

答案 0 :(得分:0)

首先,您需要为Programmers集合创建单独的集合视图源。然后像对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;

现在,替换您的列定义,如下所示:

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