在 Silverlight 中同步填充自定义控件的 DependencyProperties
我有一个具有两个属性的 Silverlight 自定义控件; 文本和 ID。 我按照下面的代码为这些创建了 DependencyProperties。
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LookupControl), new PropertyMetadata(NotifyPropertyChanged));
public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(Guid?), typeof(LookupControl), new PropertyMetadata(NotifyPropertyChanged));
public event PropertyChangedEventHandler PropertyChanged;
public static void NotifyPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var control = sender as LookupControl;
if (control != null && control.PropertyChanged != null)
{
control.PropertyChanged(control, new PropertyChangedEventArgs("Text"));
}
}
public Guid? Id
{
get { return (Guid?)GetValue(IdProperty); }
set { SetValue(IdProperty, value); }
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
在控件方法中,首先填充 Id,然后填充 Text。 我的问题是,当我绑定到此控件上的 Text 和 Id 时,我希望同步填充它们的数据,以便当在任一属性上触发 PropertyChanged 事件时,它们都已更新数据。
此时,我捕捉到 Id 何时发生更改,执行一些处理,如果需要,我将 Text 设置为新值。 但是,一旦 Id 的 OnChange 完成,控制方法就会继续并在我已经将其更改回其他内容后填充文本。
I have a Silverlight custom control with two properties; Text and Id. I have created DependencyProperties for these as per the code below.
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LookupControl), new PropertyMetadata(NotifyPropertyChanged));
public static readonly DependencyProperty IdProperty = DependencyProperty.Register("Id", typeof(Guid?), typeof(LookupControl), new PropertyMetadata(NotifyPropertyChanged));
public event PropertyChangedEventHandler PropertyChanged;
public static void NotifyPropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
{
var control = sender as LookupControl;
if (control != null && control.PropertyChanged != null)
{
control.PropertyChanged(control, new PropertyChangedEventArgs("Text"));
}
}
public Guid? Id
{
get { return (Guid?)GetValue(IdProperty); }
set { SetValue(IdProperty, value); }
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
In a control method, the Id is populated first, and then the Text. My problem is that when i bind to Text and Id on this control, I want their data to be populated synchronously, so that when a PropertyChanged event fires on either property, both of them have updated data.
At this point in time I catch when the Id has changed, performed some processing, and if required, i set the Text to a new value. But once this OnChange of Id has finished, then the control method continues and populates the Text after i have already changed it back to something else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否保存了这些值并仅在两者都拥有时才进行设置?
Cold you save the values and only set when you have both?