'WPF ListView: Inline edition on button clicked
In an WPF ListView I have ListViewItems that consist of two columns, one column is a TextBlock and the another one contains two buttons (Edit and Delete) in the same column.
Also the first row of the WPF listView contains a button for adding new ListViewItems to the ListView. This first row is only for this purpose.
I have followed the example explained here. I have adapted it to my model but it is exactly the same.
Below the DataTemplate defined in the resources and later applied to the ListView:
<DataTemplate x:Key="FirstRowTemplate1">
<Grid Width="190">
<Button Content="Add" Command="{Binding Path=DataContext.AddCommand, ElementName=MyListView}"
Width="180"
HorizontalAlignment="Center"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="NextRowTemplate1">
<TextBlock Text="{Binding PathString}"/>
</DataTemplate>
<DataTemplate x:Key="NameColumnTemplate">
<ContentPresenter x:Name="ButtonPresenter"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
ContentTemplate="{StaticResource NextRowTemplate1}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Add}" Value="True">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource FirstRowTemplate1}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Add}" Value="False">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource NextRowTemplate1}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<DataTemplate x:Key="FirstRowTemplate2"/>
<DataTemplate x:Key="NextRowTemplate2">
<StackPanel Orientation="Horizontal">
<Button Content="Edit" Command="{Binding Path=DataContext.EditCommand, ElementName=MyListView}" CommandParameter="{Binding PathString}"/>
<Button Content="-" Command="{Binding Path=DataContext.DelCommand, ElementName=MyListView}" Margin="10 0 0 0" CommandParameter="{Binding PathString}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="AddDelItemTemplate">
<ContentPresenter x:Name="ButtonPresenter"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
ContentTemplate="{StaticResource NextRowTemplate2}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Add}" Value="True">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource FirstRowTemplate2}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Add}" Value="False">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource NextRowTemplate2}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Then they are applied to the ListView:
<ListView x:Name="MyListView"
ItemsSource="{Binding pathList}"
IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Paths" Width="350" CellTemplate="{StaticResource NameColumnTemplate}"/>
<GridViewColumn Width="70" Header="Edit/Delete" CellTemplate="{StaticResource AddDelItemTemplate}"/>
</GridView>
</ListView.View>
</ListView>
Now I am trying to perform an inline edit for the TextBlock once "Edit" button is clicked for this ListViewItem and exit edition mode once user validates it by pressing return key. If user press ESC key on edit mode, it should be exit edit mode and keep the value previous editing.How can I do this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
