TwoWay手动绑定Class属性,该属性也是一个类



在下面的代码中,最初文本字段显示文本为"XYZ",但当我单击更改Address的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;
}
}
}

如果你看到我有一个person类,它是我的页面的DataContext。Person类上有一个名为Address的属性,这一点非常重要,因为我有一个称为FlatName的属性,它与表单的TextField绑定。如果我使用不更新UI的API更新FlatName。

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

这是在设置源属性时"自动"刷新目标属性所必需的。

我以前遇到过这种情况,这是因为您在绑定中使用了一个点。尝试将TextBox的datacontext设置为Address,然后只绑定到Text属性中的平面名称。

最新更新