gpt4 book ai didi

c# - 从 ViewModel 了解数据绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 10:52:44 26 4
gpt4 key购买 nike

我是 C#/.NET 的新手,尤其是绑定(bind),因为它对我来说是一个新概念,我确实理解像下面这样的简单绑定(bind)......

<TextBox Name="myTextField" Text="{Binding Path=Text, ElementName=myTextField2}" />
<TextBox Name="myTextField2" Text="{Binding Path=Text, ElementName=myTextField}"/>

但我对如何从 ViewModel 执行此操作感到困惑.我是 watching a video关于 MVVM这帮助我理解了一些我难以理解的概念,但由于没有提供代码,我尝试编写代码以查看实际演示,但我错过了 XAML/Binding部分,因为演示者没有显示该部分代码。您可以在几分钟内查看所有工作的 5:05 .

以下是所有代码:

人物类:
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace MVVM_BestPractices
{
public class Person : INotifyPropertyChanged
{
private string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
OnPropertyChanged();
OnPropertyChanged("FullName");
}
}

private string _lastName;
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
OnPropertyChanged();
OnPropertyChanged("FullName");
}
}

public string FullName
{
get { return string.Format("{0} {1}", this.FirstName, this.LastName); }
}

public Person() {}

public Person(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}

// INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}

View 模型类:
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace MVVM_BestPractices {
public class ViewModel : INotifyPropertyChanged
{
Person _model;
public Person Model
{
get { return _model; }
set
{
_model = value;
OnPropertyChanged();
}
}

public ViewModel()
{
Model = new Person("Brian", "Lagunas");
}

// INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}

}
}

XAML 代码:如您所见,我不知道如何绑定(bind)来自 ViewModel 的数据。
<Window x:Class="MVVM_BestPractices.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVM_BestPractices"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
<Grid>
<TextBox Width="200" Height="30" Margin="158,57,159,233"/>
<TextBox Width="200" Height="30" Margin="158,102,159,188"/>
<TextBlock Text="{Binding Person, Mode=OneWay}" Width="200" Height="30" Margin="158,148,159,142"/>
</Grid>
</Window>

谁能帮我将 ViewModel 中的数据绑定(bind)到 textBoxes如分钟 5:05 所示?

这将帮助我理解 BindingMVVM .

最佳答案

不看视频,我想说:

<TextBox  Text={Binding Model.FirstName} ... />
<TextBox Text={Binding Model.LastName} ... />
<TextBlock Text="{Binding Model.FullName, Mode=OneWay}" ... />

这应该可行,但只是因为您实现了 INotifyPropertyChanged在你的模型课上。你并不总是拥有或想要那个。

旁注:尽量避免使用 Margin=""出于布局目的。我知道这是设计师所做的,但这是一个非常糟糕的做法。

关于c# - 从 ViewModel 了解数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36036438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com