WPF MVVM 中 GridView 内的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21200880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:32:43  来源:igfitidea点击:

Text box inside a GridView in WPF MVVM

c#asp.netwpfgridviewmvvm

提问by Jinesh

How to bind textbox inside WPF Grid View to a property in view model? I am using MVVM pattern.

如何将 WPF 网格视图中的文本框绑定到视图模型中的属性?我正在使用 MVVM 模式。

<ListView Canvas.Left="135" Canvas.Top="185" Height="204" Name="listView1" ItemsSource="{Binding Path=lstSelectedStocks}" Width="436" >
                    <ListView.View>
                        <GridView>

                            <GridViewColumn Header="SI No." DisplayMemberBinding="{Binding Path=Index}"  Width="80"></GridViewColumn>
                            <GridViewColumn Header="Item ID" DisplayMemberBinding="{Binding Path=Itemname.ItemId}" Width="80"></GridViewColumn>
                            <GridViewColumn Header="Item Name" DisplayMemberBinding="{Binding Path=Itemname.Name}" Width="120"></GridViewColumn>
                            <GridViewColumn Header="Quantity" Width="100">
                                <GridViewColumn.CellTemplate >
                                    <DataTemplate>
                                        <TextBox x:Name="txtQuantity" Width="100"  Text="{Binding Path=Qantity, UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Header="Purchase Rate" Width="0" DisplayMemberBinding="{Binding Path=PurchasePrice}" ></GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>

Qantity is a property as defined below:

数量是定义如下的属性:

    public int Quantity;
    public int Qantity
    {
        get 
        { 
            return Quantity; 
        }
        set
        {
            Quantity = value;
            OnPropertyChanged("Qantity");
        }
    }

Anyone please help.

任何人请帮忙。

采纳答案by mrzli

I don't know if I understood you correctly (correct me if not), but based on your comments, I am guessing you want to add a quantity to a specific item.

我不知道我是否理解正确(如果没有请纠正我),但根据您的评论,我猜您想为特定项目添加数量。

If this is the case, your item class will still need to have a quantity property/field to contain this value, but you will probably want to display that property using something like a label if you don't need to be able to set that quantity directly.

如果是这种情况,您的项目类仍需要有一个数量属性/字段来包含此值,但如果您不需要设置该属性,您可能希望使用标签之类的内容来显示该属性直接量。

You should add this attribute to your ListBox in xaml:

您应该将此属性添加到 xaml 中的 ListBox 中:

SelectedItem="{Binding Path=SelectedItem}"

And to your viewmodel you need to add property called "SelectedItem" of the same type as the items in your list.

对于您的视图模型,您需要添加与列表中的项目类型相同的名为“SelectedItem”的属性。

So lets say in your viewmodel you have:

所以让我们在你的视图模型中说你有:

public List<Product> lstSelectedStocks
{
    // ...
}

This new property should be:

这个新属性应该是:

public Product SelectedItem
{
    // ...
}

Now add a new TextBox completely outside the ListBox:

现在在 ListBox 之外添加一个新的 TextBox:

You already have "Qantity" property in your viewmodel, so there no more work here.

您的视图模型中已经有“Qantity”属性,因此这里没有更多工作。

Also add a new button beside the TextBox;

同时在 TextBox 旁边添加一个新按钮;

<Button Content="Add Quantity" Command="{Binding Path=AddQuantity}" />

And you need to implement "AddQuantity" command in your viewmodel.

并且您需要在您的视图模型中实现“AddQuantity”命令。

To add a quantity to a specific item, you need to do the following steps:

要为特定项目添加数量,您需要执行以下步骤:

  1. select an item in ListBox by clicking on its row - this will set the "SelectedItem" property in your viewmodel
  2. enter the amount in the TextBox - this will set your "Quantity" property in your viewmodel
  3. press the "Add Quantity" button - this will call the "AddQuantity" command in your view model which should take the value in "Qantity" property and add that value to the SelectedItem.Quantity.
  1. 通过单击其行选择 ListBox 中的项目 - 这将在您的视图模型中设置“SelectedItem”属性
  2. 在文本框中输入数量 - 这将在您的视图模型中设置您的“数量”属性
  3. 按“添加数量”按钮 - 这将在您的视图模型中调用“添加数量”命令,该命令应该采用“数量”属性中的值并将该值添加到 SelectedItem.Quantity。

[Edit based on comment below]

[根据下面的评论编辑]

You could have the TextBox and the "AddQuantity" button on each row in the GridView.

您可以在 GridView 的每一行上都有 TextBox 和“AddQuantity”按钮。

First you would need to move this "Quantity" property to the class that implements each item. I would probably rename this to "QuantityToAdd" or something like that to avoid confusion with quantity that (probably) already exists in the item class.
You would also need to move "AddQuantity" command inside. This way you would always add value in "Qantity" (or "QuantityToAdd" if you renamed it) from that specific item, and there would probably be no need for SelectedItem="{Binding Path=SelectedItem}"in your ListView.
There is probably a more elegant solution, but this is what came to mind at this moment.

首先,您需要将此“数量”属性移动到实现每个项目的类。我可能会将其重命名为“QuantityToAdd”或类似名称,以避免与项目类中(可能)已经存在的数量混淆。
您还需要在里面移动“AddQuantity”命令。这样,您将始终从该特定项目在“Qantity”(或“QuantityToAdd”,如果您重命名它)中添加值,并且SelectedItem="{Binding Path=SelectedItem}"您的 ListView 中可能不需要。
可能有一个更优雅的解决方案,但这就是此刻想到的。

回答by SivaKumar

Binding Path you assigned to Text property is fine. Only thing you need to check Quantity Property is part of the class which you assigned to gridview itemssource as a collection.

您分配给 Text 属性的绑定路径很好。您唯一需要检查的 Quantity 属性是您作为集合分配给 gridview itemssource 的类的一部分。