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

c# – WPF DataGrid – > CellEditingTemplate显示的图标,第1张

概述当您在CellEditingTemplate中使用组合框时,单元格右侧会显示一个下拉箭头.当您使用日期选择器时,单元格的右侧会显示一个小日历. 在创建CellEditingTemplate时,如何控制此区域中显示的内容?如果您使用自定义控件并希望在此区域中显示图标,该怎么做? 您应该在自定义用户控件中添加此图标. 例: 假设我们有简单的类Person: class Person{ pub 当您在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>

结果:

总结

以上是内存溢出为你收集整理的c# – WPF DataGrid – > CellEditingTemplate显示的图标全部内容,希望文章能够帮你解决c# – WPF DataGrid – > CellEditingTemplate显示的图标所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1230057.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存