0

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.

1 Answer 1

1

You need to bind the Text property of the TextBox to the matching property of your class (Product.Owner):

<TextBox Width="90" Name="TextBox" Text="{Binding Owner, Mode=TwoWay}" />

Edit

The property is updated when the TextBox looses the focus. This behavior can be changed by setting the UpdateSourceTrigger in the binding to PropertyChanged or Explicit.

For details, see: http://msdn.microsoft.com/en-us/library/ms754356%28v=vs.110%29.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

yes but what do i need to do in when the event is fired ?
You won't be able to bind it anywhere because there is no DataContext attached to it. Follow our advice from the WPF chatroom and learn about MVVM, that will make sense once you learn it.
@redaa: You don't need the event. The binding updates the property of your class when the TextBox looses the focus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.