我想让 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>
BoolToVisibilityConverter
是 ListViewItem
的 IsSelected
属性的转换器,然后确定 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
关于c# - 可编辑的 WPF ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5652527/