代码段:在下面的这段代码中,最初的文本字段将文本显示为“ XYZ”,但是当我单击更改地址的FlatName属性的按钮时。这未反映在UI中,即文本字段未更新。
<Window x:Class="WpfApp5.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:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="132,184,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBox Text="{Binding Address.FlatName, Mode=TwoWay}" HorizontalAlignment="Left" Height="23" Margin="199,134,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>
C#代码段:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp5
{
public class Address
{
public string FlatName
{
get; set;
} = "XYZ";
}
public class Person : DependencyObject
{
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name",
typeof(string),
typeof(Person),
new FrameworkPropertyMetadata(
"",
FrameworkPropertyMetadataOptions.AffectsMeasure
));
public Address Address
{
get { return (Address)GetValue(AddressProperty); }
set { SetValue(AddressProperty, value); }
}
// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AddressProperty =
DependencyProperty.Register("Address",
typeof(Address),
typeof(Person),
new FrameworkPropertyMetadata(
new Address(),
FrameworkPropertyMetadataOptions.AffectsMeasure
));
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Person person = new Person();
public MainWindow()
{
InitializeComponent();
this.DataContext = person;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
person.Address.FlatName = "ABC";
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
//string Area = person.Address.FlatName;
}
}
}
[如果您看到我的个人类是我的Page的DataContext。那里是Person类上名为Address的属性,这非常重要的是,我有一个名为FlatName的属性,即Bindind与文本字段的形式。如果我使用API更新FlatName更新用户界面。
我之前遇到过,这是因为您在绑定中使用了点。尝试将TextBox的datacontext设置为Address,然后在Text属性中仅绑定到平面名称。
[Address
应该实现INotifyPropertyChanged
:
public class Address : INotifyPropertyChanged
{
private string _flatName = "XYZ";
public string FlatName
{
get { return _flatName; }
set { _flatName = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
设置源属性时,“自动”刷新目标属性是必需的。