后面的代码信息的控制,在Debug.Print设置一个断点( “NEWVALUE ...”)
namespace WpfStackoverflow
{
/// <summary>
/// Interaction logic for InformationControl.xaml
/// </summary>
public partial class InformationControl : UserControl
{
public static readonly DependencyProperty TimeToStartProperty;
static InformationControl()
{
//FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata("");
TimeToStartProperty = DependencyProperty.Register("TimeToStart", typeof(string), typeof(InformationControl), new UIPropertyMetadata(string.Empty, UsernamePropertyChangedCallback));
}
public string TimeToStart
{
get {
return (string)GetValue(TimeToStartProperty);
}
set {
SetValue(TimeToStartProperty, value);
}
}
public InformationControl()
{
InitializeComponent();
string temp = TimeToStart;
}
private static void UsernamePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Debug.Print("OldValue: {0}", e.OldValue);
Debug.Print("NewValue: {0}", e.NewValue);
}
}
}
主窗口XAML:
<Window x:Class="WpfStackoverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfStackoverflow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:InformationControl TimeToStart="{Binding Mine.Name}" />
</Grid>
</Window>
主窗口后面的代码:
namespace WpfStackoverflow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Parent p = new Parent();
p.Mine = new Mine();
p.Mine.Name = "Hello world";
this.DataContext = p;
}
}
}
父级:
namespace WpfStackoverflow
{
public class Parent:INotifyPropertyChanged
{
private Mine _mine;
public Mine Mine
{
get
{
return _mine;
}
set
{
_mine = value;
NotifyPropertyChanged();
}
}
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Mine : INotifyPropertyChanged
{
private string _name;
public string Name { get { return _name; }
set
{
_name = value;
NotifyPropertyChanged();
}
}
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
我真的不知道你在做什么,但是如果你看看我的例子,如果你在InformationControl的依赖属性改变回调中设置了一个断点,Mine.Name在用户控件中工作。还要注意,setter永远不会被调用,因为clr会直接绕过setter和call setvalue。
什么是您的InformationControl的DataContext?我很困惑。在ItemsControl的工作方式中,你尝试了Mine.Participants,TimeToStart在哪里? –
DataContext for InformationControl未设置。我不认为这对于依赖属性是必要的吗?关于ItemsControl的一点是Path = Object.Property正在工作,但它不适用于用户控件。谢谢! – Sherlock
您可以检查设置属性的顺序以及用户控件使用Mine.Property的时间吗?在您的用户控件已经开始加载后,您可能正在设置它。 – gavin