问题描述
我很新的WPF和测试一些东西,我想我会工作的一个应用程序,包括。我有一个2行ListView控件(绑定到一个文本框)的名称斯科特Guthrie和乔恩斯基特在里面。我想在ListView选择斯科特Guthrie的,并已将其填充文本框。我希望能够编辑文本和标签关闭,并有在ListView更新。
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.
修改:我删除了code,因为这真的没有添加任何东西的问题。
Edit:I removed the code since that really didn't add anything to the question.
推荐答案
哇,这太疯狂了复杂的你有什么就有什么。
Wow, that's crazy complicated what you've got there.
您可以做到这一点更简单。你需要一个模型来重新present程序员,视图模型举行程序员列表,以及简单的绑定拿剩下的事情。
You can do this MUCH simpler. 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.
模型:
public sealed class Programmer
{
public string Name { get; set; }
}
它非常简单。再$ P $的对象psenting一个名称的程序员。我们必须在对象中封装的名称,因为字符串是在.NET中不可改变的。如果您尝试针对字符串列表一个字符串结合,变化不会传播。
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.
程序员的集合被保持在一个ViewModel。在这种情况下,我把它称为视图模型,因为我没有想象力。该视图模型包含该视图结合反对一切。在这种情况下,程序员其列表中。
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.
public sealed class ViewModel
{
public ObservableCollection<Programmer> Programmers { get; private set; }
public ViewModel()
{
Programmers = new ObservableCollection<Programmer>();
}
}
视图模型设置为我们的观点的DataContext的。在DataContext流下来的可视化树,我们可以在任何时候对绑定。
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.
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();
}
您可以在任何你想要的方式设置DataContext的;我在这里这样做是为了简单起见。
You can set the DataContext in any way you want; I'm doing it here for simplicity's sake.
在UI,我只是对绑定程序员视图模型的列表中的ListView(DataContext的,除非另有说明,是具有约束力的根路径)。然后我对绑定ListBox的的SelectedItem文本框。您从列表中选择,然后变成的SelectedItem,我可以再更改名称的程序员。
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.
<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.
这篇关于如何绑定一个ListView的SelectedItem使用双向模式文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!