简体   繁体   English

WPF数据集DataGrid外键绑定

[英]WPF Dataset DataGrid Foreign Key Binding

I have two tables in a dataset: 我在数据集中有两个表:

Schedules
-------------
id INT
schedule_date DATETIME
service_id INT FOREIGN KEY REFERENCES services(id)

Services
-------------
id INT
name VARCHAR

I can easily bind a DataGridComboBoxColumn and get the result I want in the following way: 我可以通过以下方式轻松地绑定DataGridComboBoxColumn并获得所需的结果:

<DataGridComboBoxColumn Header="Service" 
    ItemsSource="{Binding Source={StaticResource schedulesDataSet}, Path=services}"
    DisplayMemberPath="name"
    SelectedValueBinding="{Binding Path=service_id}" 
    SelectedValuePath="id" />

But how do I do it with a DataGridTemplateColumn ? 但是,如何使用DataGridTemplateColumn呢? I want to do this: 我想做这个:

<DataGridTemplateColumn Header="Service" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=services.name}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

But as I am using a Dataset and not Entity Framework, I guess that's why it doesn't work. 但是由于我使用的是数据集而不是实体框架,所以我猜这就是为什么它不起作用。

What's the right way to do it? 什么是正确的方法?

EDIT: 编辑:

Complete DataGrid code: 完整的DataGrid代码:

<Grid DataContext="{Binding Source={StaticResource schedulesViewSource}}">
    <DataGrid Margin="0,10,0,0" Grid.Row="1" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserResizeRows="False" Name="schedulesDataGrid">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Date" Binding="{Binding Path=schedule_date,StringFormat=dd-MMM-yy,ConverterCulture=da-DK}" Width="*" />
            <DataGridTemplateColumn Header="Service" Width="*">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=services.name}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridComboBoxColumn Header="Status" Width="*" x:Name="scheduleStatusColumn" ItemsSource="{StaticResource ScheduleStatuses}" />
            <DataGridTextColumn Header="Source" Binding="{Binding Path=done_by}" Width="*" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

为您的DataGrid设置ItemsSource

   <DataGrid Margin="0,10,0,0" Grid.Row="1" ItemsSource="{Binding schedulesViewSource, Mode=OneWay}" AutoGenerateColumns="False" CanUserResizeRows="False" Name="schedulesDataGrid">

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

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