加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – WPF DataGrid – > CellEditingTemplate显示的图标

发布时间:2020-12-15 21:53:58 所属栏目:百科 来源:网络整理
导读:当您在CellEditingTemplate中使用组合框时,单元格右侧会显示一个下拉箭头.当您使用日期选择器时,单元格的右侧会显示一个小日历. 在创建CellEditingTemplate时,如何控制此区域中显示的内容?如果您使用自定义控件并希望在此区域中显示图标,该怎么做? 解决方
当您在CellEditingTemplate中使用组合框时,单元格右侧会显示一个下拉箭头.当您使用日期选择器时,单元格的右侧会显示一个小日历.

在创建CellEditingTemplate时,如何控制此区域中显示的内容?如果您使用自定义控件并希望在此区域中显示图标,该怎么做?

解决方法

您应该在自定义用户控件中添加此图标.

例:

假设我们有简单的类Person:

class Person
{
    public int ID { get; set; }
    public string Name { get; set; }
}

我们想要创建自定义控件来编辑人名.

1)我们必须在我们的应用程序中添加图标作为资源(Build Action = Resource).

在我的例子中,我创建了文件夹Images并将图标“user.png”放在那里.

2)在下一步中,我们创建自定义控件“NameUserControl”:

<UserControl x:Class="WpfApplicationDataGrid.NameUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="30" />
        </Grid.ColumnDefinitions>      

        <TextBox Text="{Binding Path=Name}" />
        <Image Source="/Images/user.png" Grid.Column="1" />
    </Grid>
</UserControl>

3)现在我们可以在CellEditingTemplate中使用新的自定义用户控件:

<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding ID}" />
        <DataGridTemplateColumn Header="Name">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <local:NameUserControl />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

结果:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读