Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从ListView WPF更新对象_C#_Wpf_Listview_Data Binding - Fatal编程技术网

C# 从ListView WPF更新对象

C# 从ListView WPF更新对象,c#,wpf,listview,data-binding,C#,Wpf,Listview,Data Binding,我有一个类产品,它与一个列表视图一起显示,在这个列表视图中,我有一个文本框,表示产品所有者。我无法实现的是从listview更新Product对象。很多人会说为什么不使用DataGrid?因为我想尝试使用listview 我的XAML代码: <Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x

我有一个类
产品
,它与一个列表视图一起显示,在这个列表视图中,我有一个文本框,表示
产品所有者
。我无法实现的是从listview更新Product对象。很多人会说为什么不使用DataGrid?因为我想尝试使用listview

我的XAML代码:

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

此按钮用于从当前产品更新ProductOwner

背后的代码:

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

    }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
宽度=系统参数。PrimaryScreenWidth-100;
高度=系统参数。主屏幕高度-100;
产品=新产品
{
Name=“name1”,
Reference=“reference1”,
Owner=“owner1”
};
产品2=新产品
{
Name=“name2”,
Reference=“reference2”,
Owner=“”
};
列表=新列表();
列表。添加(产品);
添加(产品2);
初始化组件();
ListView.ItemsSource=列表;
}
私有无效按钮\u OnClick(对象发送器,路由目标)
{
//更新产品1和产品2
}
}
在按钮OnClick事件中,文本框中的值将更新对象的值。
我知道这个问题已经在stackoverflow上了,但它不能满足我的需要。

您需要将
文本框的
文本
属性绑定到类的匹配属性(
Product.Owner
):


编辑

文本框
失去焦点时,该属性将更新。可以通过将绑定中的
UpdateSourceTrigger
设置为
PropertyChanged
Explicit
来更改此行为


有关详细信息,请参阅:

是,但在触发事件时我需要做什么?您将无法将其绑定到任何位置,因为它没有附加DataContext。按照我们在WPF聊天室的建议,了解MVVM,一旦你了解它,这将是有意义的。@redaa:你不需要这个活动。当文本框失去焦点时,绑定会更新类的属性
<TextBox Width="90" Name="TextBox" Text="{Binding Owner, Mode=TwoWay}" />