C# 获取UserControl代码中的依赖项属性值

C# 获取UserControl代码中的依赖项属性值,c#,.net,wpf,data-binding,wpf-controls,C#,.net,Wpf,Data Binding,Wpf Controls,我是WPF的新手。我面临以下问题。我有一个用户控件,下面是用户控件的代码 客户控制代码 public partial class CustomCanvas : UserControl { public CustomCanvas() { InitializeComponent(); DrawCanvas(); } private void DrawCanvas() { //TODO: //Ge

我是WPF的新手。我面临以下问题。我有一个用户控件,下面是用户控件的代码

客户控制代码

public partial class CustomCanvas : UserControl
{
    public CustomCanvas()
    {
        InitializeComponent();
        DrawCanvas();
    }

    private void DrawCanvas()
    {
        //TODO:
        //Get the Dictionary Value from Parent Bound Property
    }

    public Dictionary<string, List<Shapes>> ShapesData
    {
        get { return (Dictionary<string, List<Shapes>>)GetValue(ShapesDataProperty); }
        set { SetValue(ShapesDataProperty, value); }
    }

    public static readonly DependencyProperty ShapesDataProperty =
        DependencyProperty.Register("ShapesData", typeof(Dictionary<string, List<Shapes>>), typeof(CustomCanvas), new PropertyMetadata(ShapesDataChanged));

    private static void ShapesDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var value = e.NewValue;
    }
}
<Grid>
    <uc:CustomCanvas ShapesData="{Binding ShData}" ></uc:CustomCanvas>
</Grid>
公共部分类CustomCanvas:UserControl
{
公共自定义画布()
{
初始化组件();
画布();
}
私有void DrawCanvas()
{
//待办事项:
//从父绑定属性获取字典值
}
公共字典形状数据
{
获取{return(Dictionary)GetValue(ShapesDataProperty);}
set{SetValue(ShapesDataProperty,value);}
}
公共静态只读从属属性ShapesDataProperty=
DependencyProperty.Register(“ShapesData”、typeof(字典)、typeof(CustomCanvas)、new PropertyMetadata(ShapesDataChanged));
私有静态无效形状DataChanged(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var值=e.NewValue;
}
}
主窗口代码

public partial class CustomCanvas : UserControl
{
    public CustomCanvas()
    {
        InitializeComponent();
        DrawCanvas();
    }

    private void DrawCanvas()
    {
        //TODO:
        //Get the Dictionary Value from Parent Bound Property
    }

    public Dictionary<string, List<Shapes>> ShapesData
    {
        get { return (Dictionary<string, List<Shapes>>)GetValue(ShapesDataProperty); }
        set { SetValue(ShapesDataProperty, value); }
    }

    public static readonly DependencyProperty ShapesDataProperty =
        DependencyProperty.Register("ShapesData", typeof(Dictionary<string, List<Shapes>>), typeof(CustomCanvas), new PropertyMetadata(ShapesDataChanged));

    private static void ShapesDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var value = e.NewValue;
    }
}
<Grid>
    <uc:CustomCanvas ShapesData="{Binding ShData}" ></uc:CustomCanvas>
</Grid>

ShapesData的值绑定到以下代码

ShData = new Dictionary<string, List<Shapes>>()
        {
            {"Week 1", new List<Shapes>() {Shapes.Circle, Shapes.Rectangle}},
            {"Week 2", new List<Shapes>() {Shapes.Circle}},
            {"Week 3", new List<Shapes>() {Shapes.Rectangle}}
        };
ShData=newdictionary()
{
{“第一周”,新列表(){Shapes.Circle,Shapes.Rectangle},
{“第二周”,新列表(){Shapes.Circle},
{“第三周”,新列表(){Shapes.Rectangle}
};
现在我的问题是,在CustomControlDrawCanvas方法中,我希望获取父对象中的有界值。有谁能给我指点一下吗

p.S:我知道如何使用相对资源和模式作为FindAncestor在child中绑定此值。这里我只想获取值并处理字典数据。在ShapesDataChanged中,我可以轻松访问数据,但问题在于在DrawCanvas函数中获取数据


提前感谢。

您可以使用DependencyObject的GetValue()方法

var theValueYouNeeded=CustomCanvas.GetValue(ShapesDataProperty);
字典值=(字典)您需要的值;
....

只需使用
ShapesData
属性即可。它将返回绑定值。@ghord这是我最初的想法。但不幸的是,它不起作用,我不明白为什么它不起作用,因为它很符合逻辑。得到了答案@古德,你是对的。问题是,我没有在依赖属性更改时调用DrawCanvase。