【问题标题】:WPF datagridtextcolumn - always show textboxWPF datagridtextcolumn - 始终显示文本框
【发布时间】:2010-05-25 16:42:22
【问题描述】:

默认情况下,WPF 数据网格文本显示为标签,并在单击时进入编辑状态。有没有办法修改列以使文本框始终可见(而不是取决于单击事件)? 提前致谢, JP

【问题讨论】:

    标签: c# wpf wpfdatagrid datagridtextcolumn


    【解决方案1】:

    我根据您在评论中的说明更新了我的答案。您可以自己为单元格设置模板。下面是年龄列使用文本块的示例。

    XAML:

    <Window x:Class="GridTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
        Height="300" Width="300">
        <StackPanel>
            <Controls:DataGrid Name="dataGrid" AutoGenerateColumns="False" >
                <Controls:DataGrid.Columns>
                    <Controls:DataGridTextColumn 
                        Header="Name" 
                        Binding="{Binding Path=Name}" />
                    <Controls:DataGridTemplateColumn Header="Age">
                        <Controls:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=Age}" />
                            </DataTemplate>
                        </Controls:DataGridTemplateColumn.CellTemplate>
                        <Controls:DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=Age}" />
                            </DataTemplate>
                        </Controls:DataGridTemplateColumn.CellEditingTemplate>
                    </Controls:DataGridTemplateColumn>
                </Controls:DataGrid.Columns>
            </Controls:DataGrid>
        </StackPanel>
    </Window>
    

    后面的代码:

    using System;
    using System.Collections.Generic;
    using System.Windows;
    
    namespace GridTest
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
    
                dataGrid.ItemsSource = new List<Person>(
                    new Person[]
                    {
                        new Person("Bob", 30),
                        new Person("Sally", 24),
                        new Person("Joe", 17)
                    });
            }
        }
    
        public class Person
        {
            public String Name { get; set; }
            public int Age { get; set; }
    
            public Person(String name, int age)
            {
                Name = name;
                Age = age;
            }
        }
    }
    

    【讨论】:

    • 不,我是说datagridtextcolumn 是多态的。第一个状态是标签。单击标签启用数据输入。失去焦点将其切换回标签。在编辑特定行时,您只会看到文本框本身 - 我总是希望看到 texbox
    猜你喜欢
    • 2011-07-20
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多