WPF DataContextProxy在资源部分 [英] WPF DataContextProxy in resources section

查看:199
本文介绍了WPF DataContextProxy在资源部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的WPF应用程序中使用DataContextProxy麻烦。当我把一个DataContextProxy在网格的参考资料部分它永远不会加载。如果我移动DataContextProxy出资源部分一切正常的。

I’m having trouble using a DataContextProxy in my WPF application. When I place a DataContextProxy in the Resources section of a Grid it is never loaded. If I move the DataContextProxy out of the resource section everything works correctly.

我一直在调查这一段时间,尝试了许多方法来调试应用程序。

I’ve been investigating this for some time and have tried a number of methods to debug the application.


  • 我已经放在我试图用
    与代理的控制DebugConverter。调试器不会被调用。

  • I’ve placed a DebugConverter on the control that I’m trying to use the Proxy with. The Debug converter is never called.

我用WPFSnoop,看看是否有任何约束力的错误。我得到的
以下的DataContextProxy绑定错误,

I’ve used WPFSnoop to see if there are any binding errors. I get the following binding error on the DataContextProxy,

System.Windows.Data错误:3:找不到元素提供的DataContext。
BindingExpression:(无路径);的DataItem = NULL;目标元素是'代理'
(名称='');目标属性的DataContext(类型'对象')

System.Windows.Data Error: 3 : Cannot find element that provides DataContext. BindingExpression:(no path); DataItem=null; target element is 'Proxy' (Name=''); target property is 'DataContext' (type 'Object')

我已经放在我DataContextProxy的Loaded事件断点。
Loaded事件不会被调用,我已经放在
DataContextChanged仅事件,这是从来没有所谓的断点。

I’ve placed a breakpoint on the loaded event of my DataContextProxy. The loaded event is never called and I’ve placed a breakpoint in the DataContextChanged event which is never called.

下面是一些示例代码来证明这一点。很显然,我知道我并不真的需要在文本框使用DataContextProxy。

Here is some sample code to demonstrate this. Obviously I know I do not really need to use a DataContextProxy on the TextBox.

<Window x:Class="WpfDataContextProxyBug.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfDataContextProxyBug"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:DebugConverter x:Key="DebugConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.Resources>
            <local:Proxy x:Key="Proxy" DataContext="{Binding}" />
        </Grid.Resources>

    <TextBox DataContext="{Binding Path=Name, Source={StaticResource Proxy}, Converter={StaticResource DebugConverter}}"/>
    </Grid>
</Window>



DataContextProxy类

The DataContextProxy class

public class Proxy : FrameworkElement
{
    public Proxy()
    {
        Loaded += DataContextProxy_Loaded;
        DataContextChanged += Proxy_DataContextChanged;
    }

    void Proxy_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {

    }

    void DataContextProxy_Loaded(object sender, RoutedEventArgs e)
    {

    }

}


推荐答案

我也当我试图用DataContextProxy WPF中遇到这个问题。我想出了通过它的启发的解决方案,这似乎相当体面的处理工作。检查出来:

I've also hit this problem when I tried to use the DataContextProxy in WPF. I've come up with a solution inspired by it which seems to handle the job pretty decently. Check it out:

public class DataContextProxyBehavior : Behavior<FrameworkElement>
{
    public Object DataSource
    {
        get { return (Object)GetValue(DataSourceProperty); }
        set { SetValue(DataSourceProperty, value); }
    }
    public static readonly DependencyProperty DataSourceProperty =
        DependencyProperty.Register("DataSource", typeof(Object), typeof(DataContextProxy), null);

    protected override void OnAttached()
    {
        base.OnAttached();

        // Binds the target datacontext to the proxy,
        // so whenever it changes the proxy will be updated
        var binding = new Binding();
        binding.Source = this.AssociatedObject;
        binding.Path = new PropertyPath("DataContext");
        binding.Mode = BindingMode.OneWay;
        BindingOperations.SetBinding(this, DataContextProxyBehavior.DataSourceProperty, binding);

        // Add the proxy to the resource collection of the target
        // so it will be available to nested controls
        this.AssociatedObject.Resources.Add(
            "DataContextProxy",
            this
        );
    }
    protected override void OnDetaching()
    {
        base.OnDetaching();

        // Removes the proxy from the Resources
        this.AssociatedObject.Resources.Remove(
            "DataContextProxy"
        );
    }
}

您只需将其连接到父元素。在子元素中的静态资源引用将保持不变。我已经张贴在这里的使用示例

You just need to attach it to the parent element. The static resource reference in the child element will remain the same. I've posted an usage example here.

这篇关于WPF DataContextProxy在资源部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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