将资源绑定到视图模型 [英] Binding a Resource to a View Model

查看:93
本文介绍了将资源绑定到视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

< Grid>
< local:GroupingConsumer Name =groupingConsumer1Provider = {StaticResource groupingProvider} />
< local:GroupingConsumer Name =groupingConsumer2Provider = {StaticResource groupingProvider} />
< / Grid>
< / UserControl>

如何将 GroupValue 绑定到属性在这个观点的视图模型中。我尝试了以下内容:

 < local:GroupingProvider x:Key =groupingProviderGroupValue ={Binding ElementName = userControl,Path = DataContext.Property}/> 

但这不行。



编辑:



GroupProvider extends DependencyObject GroupValue DependencyProperty 。我收到以下错误:

  System.Windows.Data错误:2:找不到针对目标元素的FrameworkElement或FrameworkContentElement 。 BindingExpression:Path = DataContext.Property; DataItem = null;目标元素是GroupingProvider(HashCode = 47478197);目标属性是'GroupValue'(键入'TimeSpan')

这似乎表明它找不到 userControl



更多编辑:


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

解决方案

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



以下代码给出了与您相同的错误得到



不工作资源:

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


public static readonly DependencyProperty BarcodesProperty =
DependencyProperty.Register(条形码,typeof(IEnumerable< BarcodeResult>),typeof(PrintBarcodesDocumentHelper),新的PropertyMetadata(null, HandleBarcodesChanged));

private static void HandleBarcodesChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
//做东西
}
}

Xaml:

 < UserControl资源> 
<条形码:PrintBarcodesDocumentHelper x:Key =docHelper条形码={Binding BarcodeResults}/>
< /UserControl.Resources>

我的viewmodel绑定到 DataContext UserControl



错误:

  System.Windows.Data错误:2:找不到针对目标元素的FrameworkElement或FrameworkContentElement。 BindingExpression:Path = BarcodeResults; DataItem = null;目标元素为'PrintBarcodesDocumentHelper'(HashCode = 55335902);目标属性是'Barcodes'(类型'IEnumerable`1')

工作资源类:

  public class PrintBarcodesDocumentHelper:Freezable 
{
//相同的属性

protected override Freezable CreateInstanceCore()
{
返回新的PrintBarcodesDocumentHelper();
}
}

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


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 DependencyProperty. 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.

这篇关于将资源绑定到视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆