WPF DataContextProxy在资源部分(WPF DataContextProxy in

2019-07-29 06:33发布

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

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

  • 我放置在我试图使用与代理的控制DebugConverter。 调试器永远不会被调用。

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

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

  • 我已经放在我的DataContextProxy的负载情况下一个断点。 Loaded事件不会被调用,我已经放在一个断点在DataContextChanged仅事件,永远不会被调用。

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

<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类

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)
    {

    }

}

Answer 1:

当我试图用DataContextProxy在WPF我也打了这个问题。 我想出了通过它的启发的解决方案,这似乎很体面的处理工作。 看看这个:

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"
        );
    }
}

你只需将其附加到父元素。 在子元素的静态资源引用将保持不变。 我已经发布了一个使用例子在这里 。



文章来源: WPF DataContextProxy in resources section