Reputation: 27
So I have this listview with a few headers, and when the data is being set I would like to have the option to double click one of the columns to edit the value. Is this possible and if so what is the proper way of doing so?
<ListView Name="LvProducts" Margin="224,204,10,35">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="120" DisplayMemberBinding="{Binding ID}" />
<GridViewColumn Header="SKU" Width="50" DisplayMemberBinding="{Binding SKU}" />
<GridViewColumn Header="Name" Width="150" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Description" Width="150" DisplayMemberBinding="{Binding Description}" />
<GridViewColumn Header="In Stock" Width="150" DisplayMemberBinding="{Binding InStock}" />
<GridViewColumn Header="Price" Width="150" DisplayMemberBinding="{Binding Price}" />
<GridViewColumn Header="Images" Width="150" DisplayMemberBinding="{Binding Images}" />
</GridView>
</ListView.View>
</ListView>
Upvotes: 0
Views: 614
Reputation: 2392
Answered by Sajeetharan in this post how to make the column editable in ListView?
Create a customized control called EditBox for GridViewColumn.CellTemplate.
In Normal mode, a TextBlock is used to display content; In Editing mode, a TextBox will pop up for editing.
Class inhertied from Control
public class EditBox : Control
{
static EditBox()
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value",
typeof(object),
typeof(EditBox),
new FrameworkPropertyMetadata());
}
Add a dependency Property for IsEditing.
public static DependencyProperty IsEditingProperty =
DependencyProperty.Register(
"IsEditing",
typeof(bool),
typeof(EditBox),
new FrameworkPropertyMetadata(false)
);
Style for the Custom EditBox:
<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>
In your Xaml, you can place the EditBox in each column:
//Example with first column
<GridViewColumn Header="ID" Width="120" DisplayMemberBinding="{Binding ID}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<i:EditBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Upvotes: 1