将外键属性绑定到 DataGrid ViewSource
Binding Foreign Key Properties to DataGrid ViewSource
我有这个型号:
Model
我有一个通过 collectionViewSource 显示组件信息的数据网格,我想要一个 ComboBoxColumn 来显示组件的 programmer.name,并在展开组合框时显示所有其他程序员,以便用户可以更改谁来编程零件。这是我的代码:
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>
这显示了为每个组件选择的正确程序员(姓名),但是我不知道在展开组合框时如何显示其他程序员。我试过像这样绑定项目源:
<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>
但这不起作用。我的模型配置方式有问题吗?看来我不能访问程序员作为一个集合,它只能识别每个组件的一个程序员。
我看了 20 多个类似的帖子,但我不知道我错过了什么,任何方向都会很棒。
首先,您需要为 Programmers
collection 创建一个单独的 collection 查看源代码。然后像 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}" />
我有这个型号: Model 我有一个通过 collectionViewSource 显示组件信息的数据网格,我想要一个 ComboBoxColumn 来显示组件的 programmer.name,并在展开组合框时显示所有其他程序员,以便用户可以更改谁来编程零件。这是我的代码:
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>
这显示了为每个组件选择的正确程序员(姓名),但是我不知道在展开组合框时如何显示其他程序员。我试过像这样绑定项目源:
<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>
但这不起作用。我的模型配置方式有问题吗?看来我不能访问程序员作为一个集合,它只能识别每个组件的一个程序员。
我看了 20 多个类似的帖子,但我不知道我错过了什么,任何方向都会很棒。
首先,您需要为 Programmers
collection 创建一个单独的 collection 查看源代码。然后像 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}" />