Wpf 将资源绑定到视图模型

Wpf 将资源绑定到视图模型,wpf,data-binding,resources,Wpf,Data Binding,Resources,如何将视图模型中的数据绑定到用户控件资源中的对象中?下面是一个非常抽象的例子: <UserControl ... xmlns:local="clr-namespace:My.Local.Namespace" Name="userControl"> <UserControl.Resources> <local:GroupingProvider x:Key="groupingProvider"

如何将视图模型中的数据绑定到用户控件资源中的对象中?下面是一个非常抽象的例子:

<UserControl ... 
             xmlns:local="clr-namespace:My.Local.Namespace"
             Name="userControl">
    <UserControl.Resources>
        <local:GroupingProvider x:Key="groupingProvider" GroupValue="{Binding ???}" />
    </UserControl.Resources>

    <Grid>
        <local:GroupingConsumer Name="groupingConsumer1" Provider={StaticResource groupingProvider"} />
        <local:GroupingConsumer Name="groupingConsumer2" Provider={StaticResource groupingProvider"} />
    </Grid>
</UserControl>
但这不起作用

编辑:


GroupProvider
扩展了
DependencyObject
GroupValue
dependencProperty
的名称。我得到以下错误:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.Property; DataItem=null; target element is 'GroupingProvider' (HashCode=47478197); target property is 'GroupValue' (type 'TimeSpan')
这似乎表明它找不到
userControl

更多编辑:


没有人能回答我的问题?没有办法做到这一点吗?

为了启用绑定,
GroupingProvider
需要从
Freezable
FrameworkElement
FrameworkContentElement
派生,而
GroupValue
需要是
从属属性
才能启用绑定,
GroupingProvider
需要从
Freezable
FrameworkElement
FrameworkContentElement
派生,
GroupValue
需要是一个
dependencProperty

我知道有点晚了,但我也遇到了同样的问题。Ricks的答案是正确的,您需要继承自
Freezable

下面的代码给了我与您相同的错误

非工作资源:

public class PrintBarcodesDocumentHelper : DependencyObject
{
    public IEnumerable<BarcodeResult> Barcodes
    {
        get { return (IEnumerable<BarcodeResult>)GetValue(BarcodesProperty); }
        set { SetValue(BarcodesProperty, value); }
    }

    public static readonly DependencyProperty BarcodesProperty =
        DependencyProperty.Register("Barcodes", typeof(IEnumerable<BarcodeResult>), typeof(PrintBarcodesDocumentHelper), new PropertyMetadata(null, HandleBarcodesChanged));

    private static void HandleBarcodesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Do stuff
    }
}
工作资源类别:

public class PrintBarcodesDocumentHelper : Freezable
{
    // Same properties

    protected override Freezable CreateInstanceCore()
    {
        return new PrintBarcodesDocumentHelper();
    }
}

不幸的是,我不知道为什么它必须是可自由使用的,我知道有点晚了,但我也有同样的问题。Ricks的答案是正确的,您需要继承自
Freezable

下面的代码给了我与您相同的错误

非工作资源:

public class PrintBarcodesDocumentHelper : DependencyObject
{
    public IEnumerable<BarcodeResult> Barcodes
    {
        get { return (IEnumerable<BarcodeResult>)GetValue(BarcodesProperty); }
        set { SetValue(BarcodesProperty, value); }
    }

    public static readonly DependencyProperty BarcodesProperty =
        DependencyProperty.Register("Barcodes", typeof(IEnumerable<BarcodeResult>), typeof(PrintBarcodesDocumentHelper), new PropertyMetadata(null, HandleBarcodesChanged));

    private static void HandleBarcodesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        // Do stuff
    }
}
工作资源类别:

public class PrintBarcodesDocumentHelper : Freezable
{
    // Same properties

    protected override Freezable CreateInstanceCore()
    {
        return new PrintBarcodesDocumentHelper();
    }
}

不幸的是,我不知道为什么它必须是一个
Freezable

GroupProvider
扩展
DependencyObject
GroupValue
dependencProperty
的名称。我相信绑定只需要扩展
DependencyObject
;我尝试更改为
FrameworkElement
,但没有成功。阅读我上面的编辑。问题似乎在于从resources中引用数据上下文。FrameworkElement对我不起作用。仅可自由使用。
GroupProvider
扩展了
DependencyObject
GroupValue
dependencProperty
的名称。我相信绑定只需要扩展
DependencyObject
;我尝试更改为
FrameworkElement
,但没有成功。阅读我上面的编辑。问题似乎在于从resources中引用数据上下文。FrameworkElement对我不起作用。你解决过这个问题吗?如果是这样的话,你愿意和大家分享吗?你解决过这个问题吗?如果是的话,你介意分享吗?
public class PrintBarcodesDocumentHelper : Freezable
{
    // Same properties

    protected override Freezable CreateInstanceCore()
    {
        return new PrintBarcodesDocumentHelper();
    }
}