0

I am trying to change a binding value when I double click on a row. I have looked through several pages on google but have found nothing that does what I need.

Here is my code and an example of how I would want it to work. Is it possible to edit a row binding value in a listview like this?

WPF:

                <ListView x:Name="LstLinks" HorizontalAlignment="Left" Height="108" Margin="10,53,0,0" VerticalAlignment="Top" Width="641" SelectionMode="Single">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="G" Width="20" DisplayMemberBinding="{Binding LG}" />
                            <GridViewColumn Header="P" Width="20" DisplayMemberBinding="{Binding LP}" />
                            <GridViewColumn Header="Link Type" Width="100" DisplayMemberBinding="{Binding LType}"/>
                            <GridViewColumn Header="Code" Width="60" DisplayMemberBinding="{Binding LCode}"/>
                            <GridViewColumn Header="Company" Width="150" DisplayMemberBinding="{Binding LComp}"/>
                            <GridViewColumn Header="Name" Width="150" DisplayMemberBinding="{Binding LName}"/>
                            <GridViewColumn Header="Address" Width="137" DisplayMemberBinding="{Binding LAddress}"/>
                        </GridView>
                    </ListView.View>
                </ListView>

c#:

    void LstLinks_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var selItem = LstLinks.SelectedItem;

        //selItem.LP = "✓"; <-- Does not work. Cannot have ".LP"

        LstLinks.Items.Refresh();
    }
LiveKarma
  • 221
  • 2
  • 13
  • 1
    When you set property LP, are you raising property changed notification? Please post code for property LP. – Suresh Apr 22 '14 at 10:57
  • @LiveKarma you don't cast `SelectedItem` to your view model type, but assuming that it implements `INotifyPropertyChanged` interface this should work without additional refersh – dkozl Apr 22 '14 at 11:00
  • Sorry I didnt make it clear. My example does not work, its how I would like it to work. The ".LP" is me trying to reference the binding value of column 2. I have edited my question code a bit. – LiveKarma Apr 22 '14 at 11:01
  • What the type of behind each item? You need to cast `SelectedItem` to type that holds `LP` property – dkozl Apr 22 '14 at 11:02
  • http://stackoverflow.com/questions/728205/wpf-listview-attaching-a-double-click-on-an-item-event – ray Apr 22 '14 at 11:12
  • the type of each item is string if that is what you mean.... – LiveKarma Apr 22 '14 at 11:20

3 Answers3

0

I will strongly suggest not to use this way, try to follow MVVM , Based on the assumption that your properties implementing INotifyPropertyChanged

void LstLinks_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var selItem = LstLinks.SelectedItem as YourBindingClassObject;

        selItem.LP = "✓";

        LstLinks.ItemsSource = YourItemsSource;
    }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Sorry I didnt make it clear. My example does not work, its how I would like it to work. The ".LP" is me trying to reference the binding value of column 2 but ofcourse that does not work as it is my complicated than that. I have edited my question code a bit. – LiveKarma Apr 22 '14 at 11:01
  • @LiveKarma Modified the answer – Sajeetharan Apr 22 '14 at 11:04
  • Okay I will take a look into MVVM and then try your example. Will keep this post updated and mark this as the answer if it works for me. – LiveKarma Apr 22 '14 at 11:31
0

You can not change the binding of a row. You may only change the binding of the columns under which the row will be created on.

The code you provided will most likely work with the changes Sajeetharan made, assuming your binding values are correct. Since you gave us no information about your class, we can't say for sure that your particular sample will work though.

Additionally I'd like to suggest implementing INotifyPropertyChanged and providing a property which is notifying instead of changing every attribute by hand.

0x8BADF00D
  • 962
  • 1
  • 8
  • 18
0

One way to do this is for your model object(which has the property LP) extends NotificationObject(Microsoft.Practices.Prism.ViewModel) and raise a property changed notification in the setter for LP. That will take care of refreshing the view

ankhuri
  • 179
  • 11