简体   繁体   English

更改WPF ListView SelectedItem绑定值

[英]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. 我浏览了Google的几页,但没有找到满足我需求的内容。

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: 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#: 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 我强烈建议您不要使用这种方式,请尝试遵循MVVM,前提是您的属性实现了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. 假设绑定值正确,那么您提供的代码很可能将与Sajeetharan所做的更改一起使用。 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. 另外,我建议建议实现INotifyPropertyChanged并提供一个通知的属性,而不是手动更改每个属性。

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. 一种实现方法是对模型对象(具有LP属性)扩展NotificationObject(Microsoft.Practices.Prism.ViewModel)并在LP的setter中引发属性更改的通知。 That will take care of refreshing the view 这样可以刷新视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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