如何使列在ListView中可编辑?

编程入门 行业动态 更新时间:2024-10-23 07:33:12
本文介绍了如何使列在ListView中可编辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用WPF(C#).

I use WPF (C #).

我希望用户能够编辑列"说明»":

I want the user to be able to edit the column «Description»:

<ListView> <ListView.View> <GridView> <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"></GridViewColumn> <GridViewColumn Header="Number" DisplayMemberBinding="{Binding Path=Number}"></GridViewColumn> <GridViewColumn Header="Description" > <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Description}" TextWrapping="Wrap" Margin="0"></TextBlock> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> </GridView> </ListView.View> </ListView>

请告诉我,如何使列说明"可编辑?

为此目的,最好使用另一个控件?什么?

May be better to use another control for this purpose? What?

推荐答案

为GridViewColumn.CellTemplate创建一个名为EditBox的自定义控件.

Create a customized control called EditBox for GridViewColumn.CellTemplate.

在正常模式下,TextBlock用于显示内容. 在编辑模式下,将弹出一个文本框进行编辑.

In Normal mode, a TextBlock is used to display content; In Editing mode, a TextBox will pop up for editing.

从Control继承的类

Class inhertied from Control

在ListView中编辑

public class EditBox : Control { static EditBox() { public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(object), typeof(EditBox), new FrameworkPropertyMetadata()); }

为IsEditing添加依赖项属性.

Add a dependency Property for IsEditing.

public static DependencyProperty IsEditingProperty = DependencyProperty.Register( "IsEditing", typeof(bool), typeof(EditBox), new FrameworkPropertyMetadata(false) );

自定义EditBox的样式:

Style for the Custom EditBox:

<Style x:Key="{x:Type l:EditBox}" TargetType="{x:Type l:EditBox}" > <Setter Property="HorizontalAlignment" Value="Left" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type l:EditBox}"> <TextBlock x:Name="PART_TextBlockPart" Text="{Binding Path=Value, RelativeSource = {RelativeSource TemplatedParent}}"> </TextBlock> </ControlTemplate> </Setter.Value> </Setter> </Style>

在您的Xaml中,您可以放置​​EditBox:

In your Xaml, you can place the EditBox:

<GridViewColumn Header="Description" > <GridViewColumn.CellTemplate> <DataTemplate> <i:EditBox> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn>

更多推荐

如何使列在ListView中可编辑?

本文发布于:2023-11-23 01:24:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑   ListView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!