wpf 如何使用 TwoWay 模式将 Listview SelectedItem 绑定到文本框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4529242/
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
How do I bind a Listview SelectedItem to a Textbox using the TwoWay mode?
提问by Nick U
I am very new to WPF and testing some things that I would like to include in an application that I will be working on. I have a 2 row ListView (bound to a textbox) with the names Scott Guthrie and Jon Skeet in it. I am trying to select "Scott Guthrie" in the ListView and have it populate the TextBox. I want to be able to edit the text and tab off and have the ListView updated.
我对 WPF 非常陌生,正在测试一些我想包含在我将要处理的应用程序中的东西。我有一个 2 行的 ListView(绑定到一个文本框),里面有 Scott Guthrie 和 Jon Skeet 的名字。我试图在 ListView 中选择“Scott Guthrie”并让它填充 TextBox。我希望能够编辑文本和选项卡并更新 ListView。
Edit:I removed the code since that really didn't add anything to the question.
编辑:我删除了代码,因为这确实没有为问题添加任何内容。
回答by
Wow, that's really complicated what you've got there.
哇,你在那里得到的东西真的很复杂。
This can be accomplished in a very simple way. You need a model to represent the programmer, a view model to hold a list of programmers, and simple binding to take care of the rest.
这可以通过非常简单的方式完成。你需要一个代表程序员的模型,一个保存程序员列表的视图模型,以及处理其余部分的简单绑定。
The model:
该模型:
public sealed class Programmer
{
public string Name { get; set; }
}
Its very simple. An object representing a programmer with a name. We must encapsulate the name within an object because strings are immutable in .NET. If you tried binding against a single string in a list of strings, changes wouldn't propagate.
它非常简单。代表具有姓名的程序员的对象。我们必须将名称封装在一个对象中,因为字符串在 .NET 中是不可变的。如果您尝试绑定字符串列表中的单个字符串,更改将不会传播。
The collection of programmers is kept in a ViewModel. In this case, I call it ViewModel, because I have no imagination. This view model contains everything that the view binds against. In this case, its the list of programmers.
程序员的集合保存在一个 ViewModel 中。在这种情况下,我称之为 ViewModel,因为我没有想象力。此视图模型包含视图绑定的所有内容。在这种情况下,它是程序员列表。
public sealed class ViewModel
{
public ObservableCollection<Programmer> Programmers { get; private set; }
public ViewModel()
{
Programmers = new ObservableCollection<Programmer>();
}
}
The ViewModel is set as the DataContext of our view. The DataContext flows down the visual tree, and we can bind against it at any point.
ViewModel 被设置为我们视图的 DataContext。DataContext 沿着可视化树向下流动,我们可以在任何时候绑定它。
public MainWindow()
{
var vm = new ViewModel();
vm.Programmers.Add(new Programmer { Name = "Jon Skeet" });
vm.Programmers.Add(new Programmer { Name = "Scott Guthrie" });
DataContext = vm;
InitializeComponent();
}
You can set the DataContext in any way you want; I'm doing it here for simplicity's sake.
您可以以任何您想要的方式设置 DataContext;为了简单起见,我在这里这样做。
In the UI, I simply bind the ListView against the list of Programmers in the ViewModel (the DataContext, unless otherwise stated, is the root of the binding path). I then bind the TextBox against the SelectedItem of the ListBox. You select a Programmer from the list, which then becomes the SelectedItem, which I can then change the Name of.
在 UI 中,我只是将 ListView 绑定到 ViewModel 中的程序员列表(除非另有说明,否则 DataContext 是绑定路径的根)。然后我将 TextBox 绑定到 ListBox 的 SelectedItem。您从列表中选择一个 Programmer,然后它变成 SelectedItem,然后我可以更改它的 Name。
<Window
x:Class="Programmers.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:t="clr-namespace:Programmers"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox
x:Name="list"
ItemsSource="{Binding Programmers}"
DisplayMemberPath="Name" />
<TextBox
Grid.Column="1"
VerticalAlignment="Top"
Text="{Binding SelectedItem.Name, ElementName=list}" />
</Grid>
</Window>
Simple, once you get the hang of it.
很简单,一旦掌握了窍门。
回答by Gishu
This works (except that you need to validate the textbox since you can enter any text.. a dropdown might be a better choice).
这是有效的(除了您需要验证文本框,因为您可以输入任何文本......下拉菜单可能是更好的选择)。
View:
看法:
<TabItem x:Name="RightTabPage" Header="RightModel" DataContext="{Binding Right}">
<StackPanel>
<TextBox Text="{Binding SelectedGuru}"/>
<ListView SelectedItem="{Binding SelectedGuru}" ItemsSource="{Binding Gurus}"/>
</StackPanel>
</TabItem>
ViewModel:
视图模型:
public class RightViewModel
{
public RightViewModel()
{
Gurus = new[] {"Scott Guthrie", "Jon Skeet"};
SelectedGuru = Gurus.First();
}
public string SelectedGuru { get; set; }
public IEnumerable<string> Gurus{ get; set; }
}