gpt4 book ai didi

c# - TwoWay 绑定(bind)一个实现 INotifyPropertyChanged 的​​类,没有任何反应

转载 作者:太空宇宙 更新时间:2023-11-03 13:48:38 26 4
gpt4 key购买 nike

我遵循了 Microsoft 网站上的几个示例,第一个涉及 ItemsSource:

ItemsControl.ItemsSource Property第二个:

How to: Implement Property Change Notification

将我得到的两个例子合并到我的例子中:

XAML

<Window x:Class="WpfSchede.Test.ItemsSourceTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfSchede.Test"
Title="ItemsSourceTest" Height="352" Width="467">
<Window.Resources>
<c:NameList x:Key="NameListData"></c:NameList>
<DataTemplate x:Key="NameItemTemplate">
<TextBlock Width="200" Text="{Binding Path=FullName, Mode=TwoWay}" />
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding Source={StaticResource NameListData}}" ItemTemplate="{StaticResource NameItemTemplate}" Margin="12,12,120,65" Name="listBoxNames" IsSynchronizedWithCurrentItem="True">
</ListBox>
<Button Content="Change First Name" Height="23" HorizontalAlignment="Left" Margin="12,270,0,0" Name="buttonChangeName" VerticalAlignment="Top" Width="163" Click="buttonChangeName_Click" />
<Button Content="Change Full Name" Height="23" HorizontalAlignment="Left" Margin="193,270,0,0" Name="buttonChangeFullName" VerticalAlignment="Top" Width="163" Click="buttonChangeFullName_Click" />
</Grid>

XAML.CS

using System.Windows;
namespace WpfSchede.Test
{
public partial class ItemsSourceTest : Window
{
public ItemsSourceTest()
{
InitializeComponent();
}

private void buttonChangeName_Click(object sender, RoutedEventArgs e)
{
PersonName p = listBoxNames.SelectedItem as PersonName;
p.FirstName = "Andrew";
}

private void buttonChangeFullName_Click(object sender, RoutedEventArgs e)
{
PersonName p = listBoxNames.SelectedItem as PersonName;
p.FullName = "Andrea Rossi";
}
}
}

NameList 类

using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfSchede.Test
{
public class NameList : ObservableCollection<PersonName>
{
public NameList()
: base()
{
Add(new PersonName("Wil", "Cath"));
Add(new PersonName("Isak", "Dinesen"));
Add(new PersonName("Victor", "Hugo"));
Add(new PersonName("Jules", "Verne"));
Add(new PersonName("Leonardo", "Rossi"));
}
}

public class PersonName : INotifyPropertyChanged
{
private string firstName;
private string lastName;
// Define the event
public event PropertyChangedEventHandler PropertyChanged;

public PersonName(string first, string last)
{
this.firstName = first;
this.lastName = last;
}

public string FirstName
{
get { return firstName; }
set {
firstName = value;
OnPropertyChanged(value);
}
}

public string LastName
{
get { return lastName; }
set { lastName = value; }
}

public string FullName
{
set {
string full = value as string;
char[] sep = { ' ' };
string[] n = full.Split(sep);
this.FirstName = n[0];
this.LastName = n[1];
OnPropertyChanged(value);
}
get { return firstName + " " + lastName; }
}

protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}

}
}
}

我有两个问题。

首先)单击其中一个按钮时,我希望 ListBox 反射(reflect)更改,但什么也没有发生!我错过了什么?

第二)在 DataTemplate 中,我绑定(bind)到 FullName(为了示例的缘故,仅绑定(bind)到 FirstName 会更容易,但在我看来太简单了)。所以我选择绑定(bind)到属性 FullName,为此我在 NameList 类和 DataTemplate 代码中编写了自定义代码。我的代码有什么问题?在 DataTemplate 中指定 Mode=TwoWay 是否正确?

最佳答案

OnPropertyChanged(值);错了

它应该是 OnPropertyChanged("FirstName");并且还应该跟随OnPropertyChanged("全名");导致

OnPropertyChanged("FirstName");
OnPropertyChanged("FullName");

姓氏设置函数应该有

OnPropertyChanged("LastName");
OnPropertyChanged("FullName");

最后设置函数应该有

OnPropertyChanged("FullName");
OnPropertyChanged("FirstName");
OnPropertyChanged("LastName");

关于c# - TwoWay 绑定(bind)一个实现 INotifyPropertyChanged 的​​类,没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14399404/

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