在 Silverlight 中同步填充自定义控件的 DependencyProperties

发布于 2024-07-25 23:55:45 字数 1290 浏览 2 评论 0原文

我有一个具有两个属性的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔女人霸气范 2024-08-01 23:55:45

您是否保存了这些值并仅在两者都拥有时才进行设置?

    private Guid? id;
    private string text;

    public Guid?Id
    {
        get { return id; }
        set { 
            id = value;
            TrySetValue();
        }
    }
    public string Text 
    { 
        get { return text; }
        set { text = value;
        TrySetValue()} 
    }

    private void TrySetValue()
    {
        if (id != null && text != null)
        {
            SetValue(IdProperty, id);
            SetValue(TextProperty, text);
        }
    }

Cold you save the values and only set when you have both?

    private Guid? id;
    private string text;

    public Guid?Id
    {
        get { return id; }
        set { 
            id = value;
            TrySetValue();
        }
    }
    public string Text 
    { 
        get { return text; }
        set { text = value;
        TrySetValue()} 
    }

    private void TrySetValue()
    {
        if (id != null && text != null)
        {
            SetValue(IdProperty, id);
            SetValue(TextProperty, text);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文