Search This Blog

Tuesday, November 29, 2011

C# DependencyProperty assign List or Array; C# convert List to DependencyObject

Actually if you’re trying to set a List<object> to a dependency property you don’t need to do any of these things. Just make your base class inherit from DependencyObject and use SetValue() method.

public class YourClass : DependencyObject
{
    public static DependencyProperty SCProperty = DependencyProperty.RegisterAttached("SC", typeof(List<SD>), typeof(DataGrid), new PropertyMetadata(SCChanged));
                                                                   
    public static void SetSortedColumns(DependencyObject dp, object value)
    {
        dp.SetValue(SCProperty, value);
    }

    public static object GetSortedColumns(DependencyObject dp)
    {
        return dp.GetValue(SCProperty);
    }

    private static void SCChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { }
   
   
    //REST OF YOUR CODE
    [.....]
    //REST OF YOUR CODE
   
    private void SomeMethod()
    {
        SetValue(SCProperty, newValue_List);
    }
}

No comments:

Post a Comment

If you like this post, please leave a comment :)