如何使列在ListView中可编辑? [英] how to make the column editable in ListView?

查看:602
本文介绍了如何使列在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中可编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆