如何使用 TwoWay 模式将 Listview SelectedItem 绑定到文本框?

发布于 2024-10-08 19:42:55 字数 229 浏览 6 评论 0原文

我对 WPF 非常陌生,正在测试一些我想包含在我将要开发的应用程序中的东西。我有一个 2 行 ListView(绑定到文本框),其中包含 Scott Guthrie 和 Jon Skeet 的名字。我试图在 ListView 中选择“Scott 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.

Edit:I removed the code since that really didn't add anything to the question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2024-10-15 19:42:55

哇,你那里的东西真的很复杂。

这可以通过非常简单的方式来完成。您需要一个模型来表示程序员,一个视图模型来保存程序员列表,以及简单的绑定来处理其余的事情。

模型:

public sealed class Programmer
{
    public string Name { get; set; }
}

非常简单。代表具有名字的程序员的对象。我们必须将名称封装在一个对象中,因为字符串在 .NET 中是不可变的。如果您尝试绑定字符串列表中的单个字符串,则更改不会传播。

程序员的集合保存在 ViewModel 中。在这种情况下,我将其称为ViewModel,因为我没有想象力。该视图模型包含视图绑定的所有内容。在本例中,它是程序员列表。

public sealed class ViewModel
{
    public ObservableCollection<Programmer> Programmers { get; private set; }

    public ViewModel()
    {
        Programmers = new ObservableCollection<Programmer>();
    }
}

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();
}

您可以按照您想要的任何方式设置 DataContext;为了简单起见,我在这里这样做。

在 UI 中,我只需将 ListView 绑定到 ViewModel 中的程序员列表(DataContext,除非另有说明,是绑定路径的根)。然后,我将 TextBox 绑定到 ListBox 的 SelectedItem。您从列表中选择一个程序员,然后该项目将成为 SelectedItem,然后我可以更改其名称。

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

很简单,一旦你掌握了它的窍门。

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.

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>();
    }
}

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();
}

You can set the DataContext in any way you want; I'm doing it here for simplicity's sake.

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.

赢得她心 2024-10-15 19:42:55

这是可行的(除了您需要验证文本框,因为您可以输入任何文本......下拉列表可能是更好的选择)。

视图:

<TabItem x:Name="RightTabPage" Header="RightModel"  DataContext="{Binding Right}">
                    <StackPanel>
                        <TextBox Text="{Binding SelectedGuru}"/>
                        <ListView SelectedItem="{Binding SelectedGuru}" ItemsSource="{Binding Gurus}"/>
                    </StackPanel>
                </TabItem>

视图模型:

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

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; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文