【发布时间】:2017-01-16 13:52:12
【问题描述】:
我必须动态更改文本框中的值,从组合框中选择一个值,该值出现在不同的视图中。当更改依赖属性的源时,propertychangedEventHandler 值没有改变,即它保持为空,因此事件不会被触发。结果文本框中的文本没有改变。下面是代码。我已将文本框中的文本绑定到 _name 属性。
public partial class Details : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string name = "";
public Details()
{
InitializeComponent();
Name = Connector.Name;
DataContext = this;
}
public string Name
{
get { return name; }
set
{
name = value; OnPropertyChanged("Name");
}
}
protected void OnPropertyChanged(string s)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(s));
}
}
}
Xaml 代码
<StackPanel Orientation="Vertical">
<TextBlock Text="Student Details" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="16" FontWeight="Bold"> </TextBlock>
<StackPanel Margin="0,5" Orientation="Horizontal" >
<Label MinWidth="100" MaxWidth="110">Name:</Label>
<Border BorderBrush="Gray" BorderThickness="2">
<TextBox Name="nametextbox" Text="{Binding Name,Mode=TwoWay}" Width="auto" MinWidth="100" FontWeight="Black"></TextBox>
</Border>
</StackPanel>
【问题讨论】:
-
XAML 中的绑定是什么样的?
-
您有一个名为
_name的公共属性和一个名为name的公共字段?这肯定违反了命名约定! -
你能分享 XAML 代码吗
-
您提供的 XAML 代码如何链接到 UserControl 类?你能分享完整的 XAML 文件吗?怎么改属性?是来自代码吗?是从绑定到不同的控件吗?我们需要更多信息和完整的源文件。
标签: c# wpf xaml inotifypropertychanged prism-6