DataGridTemplateColumn中的ComboBox不显示SelectedItem

时间:2012-08-11 17:53:50

标签: c# wpf

我想制作包含ComboBox的自定义DataGrid列。组合框的ItemSource绑定到枚举,组合框的选择项与集合元素中选择的枚举值绑定。

这是代码

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:AnimalObservableCollection x:Key="animals">
            </local:AnimalObservableCollection>
        <ObjectDataProvider x:Key="animalEnum" MethodName="GetValues"
                            ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:AnimalType"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <DataGrid AutoGenerateColumns="False" Margin="12,12,12,101" Name="dgAnimals" CanUserAddRows="True" CanUserDeleteRows="True"  DataContext="{StaticResource animals}" ItemsSource="{Binding}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"></DataGridTextColumn>
                <DataGridTemplateColumn Header="Animal type">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox  ItemsSource="{Binding Source={StaticResource animalEnum}}" SelectedItem="{Binding Path=AnimalType}"></ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

代码隐藏

public partial class MainWindow : Window
    {
        AnimalObservableCollection animals;
        public MainWindow()
        {
            InitializeComponent();
            animals = (AnimalObservableCollection)this.FindResource("animals");
            animals.Add(new Animal(AnimalType.horse,"Rex"));

        }
    }

    public enum AnimalType { dog, cat, horse };


    class AnimalObservableCollection : ObservableCollection<Animal>
    {
        public AnimalObservableCollection()
            : base()
        {
        }
    }

    class Animal
    {
        private AnimalType animalType;
        private string name;


        public Animal()
        {
        }
        public Animal(AnimalType animalType_, string name_)
        {
            animalType = animalType_;
            name = name_;
        }

        public AnimalType AnimalType
        {
            get
            {
                return animalType;
            }
            set
            {
                animalType = value;
            }

        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }

问题在于,当组合框失去焦点时,选择项不会在DataGrid的单元格中显示,并且此单元格保持空白。如何在AnimalType列中创建单元格来显示组合框的选择?

使用DataGridComboBoxColumn时效果很好,但是我想在将来使用DataGridTemplateColumn添加更多功能。

1 个答案:

答案 0 :(得分:4)

在你的代码中,我只看到CellEditingTemplate,这意味着只有当单元格处于编辑模式时,你才为这个单元定义了模板,你还需要定义CellTemplate,当单元格处于“查看”模式时使用。像这样:

<DataGridTemplateColumn Header="Animal type">
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox  ItemsSource="{Binding Source={StaticResource animalEnum}}" 
                       SelectedItem="{Binding Path=AnimalType}"></ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock  Text="{Binding Path=AnimalType}"></TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>