微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – WPF DataGrid – > 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>

结果:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐