Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Silverlight中同步填充自定义控件的DependencyProperties_C#_Silverlight_Custom Controls_Dependency Properties - Fatal编程技术网

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

C# 在Silverlight中同步填充自定义控件的DependencyProperties,c#,silverlight,custom-controls,dependency-properties,C#,Silverlight,Custom Controls,Dependency Properties,我有一个Silverlight自定义控件,它有两个属性;文本和Id。我已经根据下面的代码为它们创建了DependencyProperties public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(LookupControl), new PropertyMetadata(NotifyPropertyChanged)); p

我有一个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,然后填充文本。我的问题是,当我绑定到此控件上的文本和Id时,我希望同步填充它们的数据,以便在任一属性上触发PropertyChanged事件时,它们都有更新的数据


此时,我捕捉到Id已更改的时间,执行一些处理,如果需要,我将文本设置为新值。但是一旦完成了对Id的一次性更改,那么在我已经将其更改回其他内容之后,控制方法将继续并填充文本

Cold是否保存值并仅在同时具有这两个值时进行设置

    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);
        }
    }

是否保存值并仅在两者都有时设置

    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);
        }
    }