如何:使用可编辑的单元格创建 ListView

更新:2007 年 11 月

此示例演示如何在具有可编辑单元格的 GridView 视图模式中创建 ListView 控件。

示例

若要在 GridView 中编辑 GridViewColumn 的单元格,请定义要用作列的 CellTemplate 的自定义控件。

下面的示例演示名为 EditBox 的自定义控件,该控件实现两个依赖项属性:Value 和 IsEditing。Value 属性存储单元格的值。IsEditing 属性指定单元格当前是否可编辑。

public class EditBox : Control
{


...


public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register(
                "Value",
                typeof(object),
                typeof(EditBox),
                new FrameworkPropertyMetadata(null));


...


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


...


}

下面的示例为 EditBox 控件创建 Style

<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>

若要创建 TextBox 控件以编辑单元格,请实现 Adorner。下面的示例演示 EditBoxAdorner 的构造函数。

internal sealed class EditBoxAdorner : Adorner
{


...


public EditBoxAdorner(UIElement adornedElement, 
                      UIElement adorningElement): base(adornedElement)
{
    _textBox = adorningElement as TextBox;
    Debug.Assert(_textBox != null, "No TextBox!");

    _visualChildren = new VisualCollection(this);

    BuildTextBox();
}


...


}

若要控制 EditBox 何时可编辑,请使用 MouseUpMouseLeaveMouseEnter 等事件。下面的示例演示由 EditBox 收到的第一个 MouseUp 事件如何选择 EditBox,以及第二个 MouseUp 事件如何将 EditBox 置于编辑模式。

public class EditBox : Control
{


...


protected override void OnMouseUp(MouseButtonEventArgs e)
{
    base.OnMouseUp(e);

    if (e.ChangedButton == MouseButton.Right || 
        e.ChangedButton == MouseButton.Middle)
        return;

    if (!IsEditing)
    {
        if (!e.Handled && (_canBeEdit || _isMouseWithinScope))
        {
            IsEditing = true;
        }

        //If the first MouseUp event selects the parent ListViewItem,
        //then the second MouseUp event puts the EditBox in editing 
        //mode
        if (IsParentSelected)
            _isMouseWithinScope = true;
    }
}


...


}

下面的示例演示如何使用 MouseEnterMouseLeave 事件来确定单元格是否可以进行编辑。

public class EditBox : Control
{


...


protected override void OnMouseEnter(MouseEventArgs e)
{
    base.OnMouseEnter(e);
    if (!IsEditing && IsParentSelected)
    {
        _canBeEdit = true;
    }
}


...


protected override void OnMouseLeave(MouseEventArgs e)
{
    base.OnMouseLeave(e);
    _isMouseWithinScope = false;
    _canBeEdit = false;
}


...


}

若要定义允许编辑的 GridViewColumn,请将 CellTemplate 属性设置为 EditBox 控件。下面的示例指定作为 EditBox 控件的 GridViewColumnCellTemplate 属性。

<GridViewColumn Header="ID" Width="50" >
  <GridViewColumn.CellTemplate>
    <DataTemplate>
      <l:EditBox Height="25" Value="{Binding Path=EmployeeNumber}" />
    </DataTemplate>
  </GridViewColumn.CellTemplate>
</GridViewColumn>

有关完整示例,请参见 具有编辑功能的 ListView 的示例

请参见

概念

GridView 概述

参考

Control

ListView

GridView

其他资源

ListView 示例

ListView 帮助主题