wpf 如何在 ListView 中使列可编辑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23903970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:47:49  来源:igfitidea点击:

how to make the column editable in ListView?

wpflistviewwpf-controls

提问by Olga

I use WPF (C #).

我使用 WPF (C#)。

I want the user to be able to editthe 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>

Please tell me, how to make the column ?Description? editable?

请告诉我,如何制作专栏?说明?可编辑

May be better to use another controlfor this purpose? What?

为此目的使用另一个控件可能会更好?什么?

回答by Sajeetharan

Create a customized control called EditBox for GridViewColumn.CellTemplate.

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

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

在 Normal 模式下,TextBlock 用于显示内容;在编辑模式下,会弹出一个文本框进行编辑。

Class inhertied from Control

继承自 Control 的类

Editing In ListView

在 ListView 中编辑

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

Add a dependency Property for IsEditing.

为 IsEditing 添加依赖属性。

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

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>

In your Xaml, you can place the EditBox:

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

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

回答by Shigil P V

you can change that TextBlock to TextBox as below`

您可以将该 TextBlock 更改为 TextBox,如下所示`

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

</GridView>

`

`