c# - 可编辑的 WPF ListView

标签 c# wpf listview .net-3.5

我想让 ListView 在用户双击某个项目时可编辑。我知道网上有很多示例,但是,这些都是基于 IsSelected 属性,而不是处理双击事件。

有什么想法或建议吗?

更新:
因此,我遇到的另一个问题是,我如何才能访问相关 ListViewitem 的数据模板中的控件?我迷路的地方是我需要找到控件,并根据项目是否正在编辑来启用或禁用控件。

目前,ListView 数据模板如下所示:

<DataTemplate>
    <Grid>
        <TextBlock Width="180" Text="{Binding Path=Description}"
              Style="{StaticResource GridBlockStyle}" />
         <TextBox Width="180" Text="{Binding Path=Description}" 
              Style="{StaticResource GridEditStyle}" />
    </Grid>
</DataTemplate>

被引用的样式如下所示:

<Window.Resources>
    <Style TargetType="{x:Type TextBlock}" x:Key="GridBlockStyle">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Visibility" Value="{Binding Path=IsSelected,
             RelativeSource={RelativeSource FindAncestor, 
             AncestorType={x:Type ListViewItem}}, 
             Converter={StaticResource boolToVis}, 
             ConverterParameter=False}" />
    </Style>
    <Style TargetType="{x:Type FrameworkElement}" x:Key="GridEditStyle">
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="Visibility" Value="{Binding Path=IsSelected, 
            RelativeSource={RelativeSource FindAncestor,
            AncestorType={x:Type ListViewItem}},
            Converter={StaticResource boolToVis},
            ConverterParameter=True}" />
    </Style>
</Window.Resources>

BoolToVisibilityConverterListViewItemIsSelected 属性的转换器,然后确定 Visibility ListView 项。

正如您从当前 XAML 标记中看到的那样,编辑控件将在项目选择时激活(变得可见),而不是在项目双击时激活。

更新 2: 到目前为止的所有建议只让我完成了一半的解决方案。有没有人有一个可行的解决方案来解决如何访问我需要使其可见/不可见的实际控件?我会将该答案标记为解决方案!

最佳答案

如果您处于 MVVM 模式中,您可能希望查看 AttachedProperties 和行为而不是事件处理程序,以避免在代码后面编写代码。

http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx

http://blog.fossmo.net/post/How-to-create-an-attached-property-in-WPF-using-a-ComboBox.aspx

http://msdn.microsoft.com/en-us/library/ms749011.aspx

关于c# - 可编辑的 WPF ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5652527/

相关文章:

onCreateContextMenu 和 onContextItemSelected 中的 Android NULL menuInfo 仅在手动调用 onListItemClick 中的 openContextMenu 时。长按有效

c# - 以编程方式选择 ListView 中的项目

listview - BlackBerry Cascades 中的 ListView 崩溃

c# - SQLite - 检索最后一行的ID

C#:如何将字符串散列为 RIPEMD160

c# - 如何从 ViewModel 创建和显示弹出窗口

c# - 从绑定(bind)的 ItemsControl 访问我的 View

c# - 如果调试文件夹的其余部分可用,但我的 EXE 运行良好,但不是独立的

c# - 声明并初始化变量以分配匿名类型列表

c# - Visual Studio 颜色属性编辑器中的自定义调色板