简体   繁体   中英

Change WPF ListView SelectedItem binding value

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();
    }

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;
    }

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM