DataGridTemplateColumn 中的自定义控件

发布于 2024-10-09 14:21:44 字数 2799 浏览 6 评论 0原文

我想将自定义控件添加到数据网格的模板列中。

自定义控件与文本框非常相似,但其中有一个图标。用户可以单击该图标,在提示窗口中选择一项,则所选项目将被填充到文本框中。

我的问题是当文本框被填充时,单击第二列后,文本将消失。如果我用简单的文本框替换自定义控件,结果是相同的。

以下是示例代码:

 //Employee.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace SimpleGridTest
    {
        public class Employee
        {
            public string Department { get; set; }
            public int ID { get; set; }
            public string Name { get; set; }
        }
    }

Mainwindow.xaml

<Window x:Class="SimpleGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="grid" Grid.Row="1" Margin="5" AutoGenerateColumns="False"
                  RowHeight="25" RowHeaderWidth="10" 
                  ItemsSource="{Binding}"
                  CanUserAddRows="True" CanUserSortColumns="False">

            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Department" Width="150">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Department}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"                                    
                                    Width="100"/>

                <DataGridTextColumn Header="Name" 
                                    Binding="{Binding Path=Name}"
                                    Width="200"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System.Collections.ObjectModel;

namespace SimpleGridTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();

        public ObservableCollection<Employee> Employees
        {
            get { return _employees; }
            set { _employees = value; }
        }

        public MainWindow()
        {
            InitializeComponent();
            grid.ItemsSource = Employees;
        }
    }
}

如何解决此问题?或者我需要将 DataGrid***Column 编写为 DataGridTextColumn?提前致谢!

最好的问候,

约翰逊

I'd like to add my custom control into a template column of data grid.

The custom control is very similar to a text box, but has an icon in it. The user can click the icon, and selects an item from a prompted window, then the selected item will be filled into the text box.

My problem is when the text box is filled, after I click the second column, the text will disappear. If I replace the custom control with a simple text box, the result is the same.

Here is the sample code:

 //Employee.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace SimpleGridTest
    {
        public class Employee
        {
            public string Department { get; set; }
            public int ID { get; set; }
            public string Name { get; set; }
        }
    }

Mainwindow.xaml

<Window x:Class="SimpleGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="grid" Grid.Row="1" Margin="5" AutoGenerateColumns="False"
                  RowHeight="25" RowHeaderWidth="10" 
                  ItemsSource="{Binding}"
                  CanUserAddRows="True" CanUserSortColumns="False">

            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Department" Width="150">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Department}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"                                    
                                    Width="100"/>

                <DataGridTextColumn Header="Name" 
                                    Binding="{Binding Path=Name}"
                                    Width="200"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System.Collections.ObjectModel;

namespace SimpleGridTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();

        public ObservableCollection<Employee> Employees
        {
            get { return _employees; }
            set { _employees = value; }
        }

        public MainWindow()
        {
            InitializeComponent();
            grid.ItemsSource = Employees;
        }
    }
}

How can I fix this problem? Or I need to write a DataGrid***Column as DataGridTextColumn? Thanks in advance!

Best Regards,

Johnson

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早乙女 2024-10-16 14:21:44

我想您必须指定一个 CellEditingTemplate 进行编辑并显示内容,您必须指定一个普通的单元模板

             <Controls:DataGridTemplateColumn Header="Department" Width="150">
                <Controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Department}" />
                    </DataTemplate>
                </Controls:DataGridTemplateColumn.CellTemplate>
                <Controls:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Department}"/>
                    </DataTemplate>
                </Controls:DataGridTemplateColumn.CellEditingTemplate>
             </Controls:DataGridTemplateColumn>

希望这会有所帮助。

I guess you have to specify a CellEditingTemplate for editing and to display content you have to specify a normal celltemplate

             <Controls:DataGridTemplateColumn Header="Department" Width="150">
                <Controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Department}" />
                    </DataTemplate>
                </Controls:DataGridTemplateColumn.CellTemplate>
                <Controls:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Department}"/>
                    </DataTemplate>
                </Controls:DataGridTemplateColumn.CellEditingTemplate>
             </Controls:DataGridTemplateColumn>

Hope this helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文