C# 使用DependencyProperties时WPF TextBlock未更新
TextBlock显示默认的属性元数据,但不在DependencyProperty更改时更新 看起来视图没有更新,因为在分配新值后读取dependency属性将返回新值 以下是代码,请提前感谢: Data.cs:C# 使用DependencyProperties时WPF TextBlock未更新,c#,wpf,xaml,data-binding,dependency-properties,C#,Wpf,Xaml,Data Binding,Dependency Properties,TextBlock显示默认的属性元数据,但不在DependencyProperty更改时更新 看起来视图没有更新,因为在分配新值后读取dependency属性将返回新值 以下是代码,请提前感谢: Data.cs: using System.Windows; namespace RMS.Kernel { public class Data : DependencyObject { private delegate void
using System.Windows;
namespace RMS.Kernel
{
public class Data : DependencyObject
{
private delegate void ObjectDelegate(object obj);
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(string), typeof(Data), new PropertyMetadata("start text"));
private string message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public Data() { }
//singleton, make every method accesible anywhere. Only one gui object exists on runtime...
private static Data shared;
public static Data getShared()
{
if (shared == null)
{
shared = new Data();
}
return shared;
}
public void setProperties(string message)
{
this.message = message;
}
}
}
XAML:
C#属性和依赖项属性的大小写不匹配。尝试将消息
重命名为消息
。属性必须是公共的,而不是私有的,即公共字符串消息
。很抱歉,我尝试了两者,两者都不起作用。我自己也有此问题。依赖项设置正确,但未更新任何内容。像这样的时刻让我讨厌WPF。C#属性和依赖属性的情况不匹配。尝试将消息
重命名为消息
。属性必须是公共的,而不是私有的,即公共字符串消息
。很抱歉,我尝试了两者,两者都不起作用。我自己也有此问题。依赖项设置正确,但未更新任何内容。像这样的时刻让我讨厌WPF。
<local:CustomWindow x:Class="RMS.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:RMS"
xmlns:control="clr-namespace:RMS.Controls"
xmlns:kernel="clr-namespace:RMS.Kernel"
Title="MainWindow" Height="750" Width="1200">
<Window.DataContext>
<kernel:Data/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<control:CustomToolBar x:Name="customToolBar" VerticalAlignment="Top" Grid.Row="0"/>
<TextBlock Text="{Binding Path=Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="White" FontSize="12pt" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1"/>
<control:CustomStatusBar x:Name="CustomStatusBar" VerticalAlignment="Bottom" Grid.Row="2"/>
</Grid>
</local:CustomWindow>