使用ComboBox使用外键加载DataGrid行

时间:2016-09-09 14:08:35

标签: c# wpf data-binding datagrid

我试图将一系列行加载到DataGrid中,其中包含其他表的外键。在这种特定情况下,行具有TransactionTypeID(TransactionTypes表的外键),我喜欢我的ComboBox(ItemsSource是来自Transactions表的DataView类型),可以正确显示并允许修改。

<DataGridTemplateColumn Header="Transaction Types">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="Need ComboBox SelectedText here!"></TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding DataContext.TransactionTypes, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                DisplayMemberPath="Description"
                SelectedValuePath="ID"
                SelectedItem="{Binding TransactionTypeID}"></ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

ComboBox SelectedItem似乎无法正常工作,我对如何填充TextBlock文本毫无头绪。

1 个答案:

答案 0 :(得分:0)

我的理解是您要设置列的值并根据下拉列表中的特定选项显示事务类型说明。此示例显示在从编辑模板的下拉列表中选择值后绑定文本块中的文本。

XAML:

<Grid DataContext="{x:Static local:dc.Instance}">
    <Grid.Resources>
        <DataTemplate x:Key="DisplayTemplate" >
            <TextBlock Text="{Binding TransactionType.Description}"></TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="EditTemplate">
            <ComboBox ItemsSource="{x:Static local:dc.TransactionTypes}"
                DisplayMemberPath="Description"
                SelectedItem="{Binding TransactionType}">
            </ComboBox>
        </DataTemplate>
    </Grid.Resources>
    <DataGrid Name="DG1" ItemsSource="{Binding Path=DGItems}" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Transaction Type" 
                                    CellTemplate="{StaticResource DisplayTemplate}" 
                                    CellEditingTemplate="{StaticResource EditTemplate}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

数据上下文类:

public class dc
{
    private static readonly dc _dc = new dc();
    public static dc Instance { get { return _dc;  } }


    public class TransactionTypeItem
    {
        public int ID { get; set; }
        public string Description { get; set; }
    }

    public static List<TransactionTypeItem> TransactionTypes
    {
        get
        {
            return new List<TransactionTypeItem>() { new TransactionTypeItem() { ID = 0, Description = "Zero" },
                                                    new TransactionTypeItem() { ID = 1, Description = "One" },
                                                    new TransactionTypeItem() { ID = 2, Description = "Two" } };
        }
    }

    public class DataItem
    {
        public TransactionTypeItem TransactionType { get; set; }
        public string OtherData { get; set; }
    }


    private List<DataItem> _items;

    public List<DataItem> DGItems
    {
        get
        {
            return _items;
        }
    }




    private dc()
    {
        _items = new List<DataItem>()
        {
            new DataItem() { OtherData = "Test0", TransactionType = TransactionTypes[0] },
            new DataItem() { OtherData = "Test1", TransactionType = TransactionTypes[1] },
            new DataItem() { OtherData = "Test2", TransactionType = TransactionTypes[2] },
        };
    }

}