i have a class Product that is displayed with a listview, in this listview i have a textbox that represent the ProductOwner. What i can't achieve is to update the Product Object from the listview. many people will say why not using DataGrid? because i want to try with a listview.
My XAML code:
<Window
x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid>
<ListView Name="ListView">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Reference" Width="100" DisplayMemberBinding="{Binding Reference}" />
<GridViewColumn Header="Owner" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="90" Name="TextBox"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Name="Button" Content="Submit" Width="120" Height="25" Background="Azure" Click="Button_OnClick"></Button>
</Grid>
the button is here to update the ProductOwner from the current Product.
The code Behind:
public partial class MainWindow : Window
{
public MainWindow()
{
Width = SystemParameters.PrimaryScreenWidth - 100;
Height = SystemParameters.PrimaryScreenHeight - 100;
Product product = new Product
{
Name = "name1",
Reference = "reference1",
Owner = "owner1"
};
Product product2 = new Product
{
Name = "name2",
Reference = "reference2",
Owner = ""
};
List<Product> list = new List<Product>();
list.Add(product);
list.Add(product2);
InitializeComponent();
ListView.ItemsSource = list;
}
private void Button_OnClick(object sender, RoutedEventArgs e)
{
//update product1 and product2
}
}
In the Button OnClick event, the value in the textbox will update the value of the object. I know this question is already on stackoverflow, but it doesn't cover my needs.