後面的代碼信息的控制,在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