我正在学习Dependency Properties
.
我Dependency Property
在 aUserControl
和我的 中创建了一个MainWindow
,我创建了一个控件实例并设置了值。这按预期工作。
我的问题是当我尝试使用Binding
.
所以,我的 MainWindow XAML 看起来像(StartTime 类型是字符串)
<Window x:Class="TimeLineCanvas.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:timeline="clr-namespace:TimeLineCanvas.UserControls"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel>
<TextBlock Text="{Binding StartTime}" Height="100" /><!--Works binding to local property-->
<timeline:TimeLine StartTime="28/01/2015" Height="100" /><!--Works when hard coded value-->
<timeline:TimeLine StartTime="{Binding StartTime, UpdateSourceTrigger=PropertyChanged}" Height="100" /><!-- Does not display anything -->
<timeline:TimeLine x:Name="TimeLineInXaml" Height="100" /><!-- Works (value set in code behind) -->
</StackPanel>
</Grid>
</Window>
主窗口.xaml.cs
using System;
using System.Windows;
using System.ComponentModel;
namespace TimeLineCanvas
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region Constructors
public MainWindow()
{
InitializeComponent();
SetStartTime(DateTime.Now);
TimeLineInXaml.StartTime = _startTime;
this.DataContext = this;
}
#endregion
#region Properties
private string _startTime;
public string StartTime
{
get
{
return _startTime;
}
set
{
_startTime = value;
OnPropertyChanged("StartTime");
}
}
#endregion
#region Methods
internal void SetStartTime(DateTime dt)
{
this.StartTime = dt.ToShortDateString();
}
#endregion
#region INotifyPropertyChanged Implementation
public void OnPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion INotify
}
}
我的用户控件
<UserControl x:Class="TimeLineCanvas.UserControls.TimeLine"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Canvas>
<TextBlock Text="{Binding StartTime, UpdateSourceTrigger=PropertyChanged}" />
</Canvas>
</Grid>
</UserControl>
以及我的 UserControl 中的代码
using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
namespace TimeLineCanvas.UserControls
{
/// <summary>
/// Interaction logic for TimeLine.xaml
/// </summary>
public partial class TimeLine : UserControl, INotifyPropertyChanged
{
#region Constructor
public TimeLine()
{
InitializeComponent();
this.DataContext = this;
}
#endregion
#region Dependancy Properties
public static readonly DependencyProperty StartTimeProperty = DependencyProperty.Register(
"StartTime",
typeof(string),
typeof(TimeLine));
#endregion
#region Properties
public string StartTime
{
get { return (string)GetValue(TimeLine.StartTimeProperty); }
set
{
DateTime result;
if (!DateTime.TryParse(value, out result))
System.Diagnostics.Debug.Assert(false, "Expected a value which can be converted to a DateTime");
SetValue(TimeLine.StartTimeProperty, value);
}
}
#endregion
#region INotifyPropertyChanged Implementation
public void OnPropertyChanged(string PropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion INotify
}
}
所以,问题很清楚,它是绑定,但我不知道如何解决这个问题!即使我基于其他建议(例如添加DataContext="{Binding RelativeSource={RelativeSource Self}}
到 UserControl、将 INotifyPropertyChanged 添加到 MainWindow 和 UserContorl)的 Voodoo 编程(以随机顺序尝试任何东西)也不会影响结果。
我做错了什么,或者我试图做一些不该做的事情?