gpt4 book ai didi

c# - 数据绑定(bind)问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:10 24 4
gpt4 key购买 nike

我在将我的数据从 TextBox 绑定(bind)到 ViewModel 到 TextBlock 时遇到了一个主要问题。我已经像这样设置了以下 Xaml 代码:

<Page
x:Class="digiBottle.MainPage"
DataContext="{Binding Source=UserProfile}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:digiBottle"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding Source=UserProfile}">

<TextBlock HorizontalAlignment="Left" Margin="219,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="32" Width="232" Text="{Binding userFirstName, Mode=OneWay}"/>
<TextBox HorizontalAlignment="Left" Margin="39,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="111" Text="{Binding userFirstName, UpdateSourceTrigger=PropertyChanged}"/>

</Grid>

我尝试用作源的 .cs 文件定义如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace digiBottle.Model

{ 公共(public)类 UserProfile:INotifyPropertyChanged {

    public int ID { get; set; }
public string userFirstName;
public string userLastName { get; set; }
public int age { get; set; }
public int weight { get; set; }
public int height { get; set; }
public DateTime signupTime { get; set; }

public event PropertyChangedEventHandler PropertyChanged;

public UserProfile()
{
userFirstName = "First Name";
}


private void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

}

public UserProfile getCopy()
{
UserProfile copy = (UserProfile)this.MemberwiseClone();
return copy;
}
}
}

尝试将我的 TextBox 和 TextBlock 绑定(bind)到 UserProfile.cs 源中的 userFirstName 时,我做错了什么。任何帮助都是很大的帮助!

谢谢

最佳答案

我在这里注意到的第一件事是您的属性(setter)没有引发事件变化。您需要在属性 setter 中调用 RaisePropertyChanged。

我会这样写

私有(private)领域

private String _userFirstName;

然后在构造函数中

 public UserProfile()
{
this._userFirstName = "First Name";
}

随着属性(property)筹集事件

public String UserFirstName
{
get { return this._userFirstName; }
set
{
this._userFirstName = value;
this.RaisePropertyChanged("UserFirstName");
}
}

然后在 XAML 中,通过双向绑定(bind)将其与属性“UserFirstName”绑定(bind)

<TextBlock HorizontalAlignment="Left" Margin="219,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="32" Width="232" Text="{Binding UserFirstName, Mode=OneWay}"/>
<TextBox HorizontalAlignment="Left" Margin="39,72,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="111" Text="{Binding UserFirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

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

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