Binding a Resource to a View Model
How do you bind data from the view model into an object in the resources of the user control? Here is a very abstract example:
<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>
How do I bind GroupValue to a property in the view model behind this view.  I've tried the following:
<local:GroupingProvider x:Key="groupingProvider" GroupValue="{Binding ElementName=userControl, Path=DataContext.Property}"/>
But this doesn't work.
Edit:
GroupProvider extends DependencyObject and GroupValue is the name of a DependencyProper开发者_开发技巧ty.  I'm getting the following error:
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')
This seems to suggest that it cannot find userControl.
More Edit:
Nobody has an answer to my question? Is there not a way to do this?
I know its a bit late, but i had the same problem. Ricks answer is right, you need to inherit from Freezable.
The following Code gave me the same error as you got
Not working resource:
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
    }
}
Xaml:
<UserControl.Resources>
    <Barcodes:PrintBarcodesDocumentHelper x:Key="docHelper" Barcodes="{Binding BarcodeResults}"/>
</UserControl.Resources>
My viewmodel is bound to the DataContext of the UserControl.
Error:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=BarcodeResults; DataItem=null; target element is 'PrintBarcodesDocumentHelper' (HashCode=55335902); target property is 'Barcodes' (type 'IEnumerable`1')
Working resource class:
public class PrintBarcodesDocumentHelper : Freezable
{
    // Same properties
    protected override Freezable CreateInstanceCore()
    {
        return new PrintBarcodesDocumentHelper();
    }
}
Unfortunately i dont know why it have to be a Freezable.
In order to enable binding, GroupingProvider needs to be derived from Freezable or FrameworkElement or FrameworkContentElement and GroupValue needs to be a DependencyProperty.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论