DataGridTemplateColumn 中的 ComboBox 不显示 SelectedItem

2024-04-19

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

这是代码

<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 的单元格中,并且该单元格保持空白。如何使动物类型列中的单元格显示组合框的选定项目?

当我使用 DataGridComboBoxColumn 时它工作得很好,但我想使用 DataGridTemplateColumn 在将来添加更多功能。


在您的代码中,我只看到 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DataGridTemplateColumn 中的 ComboBox 不显示 SelectedItem 的相关文章

随机推荐