I'd like to make a ListView
editable when the user double clicks on an item. I realize that there are many samples out there on the web, however, these are all based on the IsSelected
property, and not on handling the double-click event.
Any ideas or pointers?
UPDATE:
So one other problem that I have is, how can I get to the controls in the data template for theListViewitem
in question? Where I'm getting lost is the point where I need to get to the controls, and either enable or disable the control, based on whether the item is being edited or not.
Currently, the ListView
data template looks like this:
<DataTemplate>
<Grid>
<TextBlock Width="180" Text="{Binding Path=Description}"
Style="{StaticResource GridBlockStyle}" />
<TextBox Width="180" Text="{Binding Path=Description}"
Style="{StaticResource GridEditStyle}" />
</Grid>
</DataTemplate>
The styles being referenced look like this:
<Window.Resources>
<Style TargetType="{x:Type TextBlock}" x:Key="GridBlockStyle">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Visibility" Value="{Binding Path=IsSelected,
RelativeSource={RelativeS开发者_如何学Goource 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>
The BoolToVisibilityConverter
is the converter for the IsSelected
property of the ListViewItem
, which then determines the Visibility
of the ListViewItem.
As you can see from the current XAML markup, the edit controls will activate (become visible) on item selection, not item double-click.
UPDATE 2: All the suggestions so far bring me to only half the way of the solution. Does anyone have a workable solution for how to access the actual controls that I need to make visible/invisible? I will mark that answer as the solution!
If you are in a MVVM pattern you may want to look at AttachedProperties and behaviors rather than event handlers to avoid writing code in your code behind.
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
Pointers:
WPF ListView: Attaching a double-click (on an item) event
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3d0eaa54-09a9-4c51-8677-8e90577e7bac/
Get the item doubleclick event of listview
http://kevin-berridge.blogspot.com/2008/06/wpf-listboxitem-double-click.html
For MVVM,
Firing a double click event from a WPF ListView item using MVVM
精彩评论