Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF与Silverlight-资源中的数据绑定_Wpf_Silverlight_Binding - Fatal编程技术网

WPF与Silverlight-资源中的数据绑定

WPF与Silverlight-资源中的数据绑定,wpf,silverlight,binding,Wpf,Silverlight,Binding,经过一段时间的silverlight开发,我目前正在做一些WPF工作 我经常用这个技巧让我的生活在我的一些领域变得更轻松: public class MyCovnerterWithDataContext : FrameworkElement, IValueConverter { private MyDataContextType Data { get { return this.DataContext as MyDataContextType; } }

经过一段时间的silverlight开发,我目前正在做一些WPF工作

我经常用这个技巧让我的生活在我的一些领域变得更轻松:

public class MyCovnerterWithDataContext : FrameworkElement, IValueConverter
{
    private MyDataContextType Data
    { 
        get { return this.DataContext as MyDataContextType; }
    }
    ....
现在我可以通过Converter方法访问我的DataContext,这在很多情况下都很方便,正如您所想象的

我在WPF中尝试了同样的技巧,结果发现,不幸的是,这根本不起作用。调试输出中存在以下错误:

“找不到提供DataContext的元素”

我假设资源不是WPF中可视化树的一部分,而它们在Silverlight中

那么-我的小把戏在WPF中也可能吗? 我的小把戏被认为是肮脏的黑客吗

你的意见和建议是什么

问候 约翰尼斯

更新: 应要求提供更多信息-实际上是一个最小的示例:

XAML:


FrameworkElement
派生的类型可能有问题: 从

[…]需要可共享[…] 从UIElement类型派生的任何对象本质上都不是 可共享[…]

DependencyObject
派生:

public class TestWrapper : DependencyObject {}

有趣。有了一个存根MyDataContextType,它编译并为我工作(前Silverlight guy)。有关此IValueConverter尝试执行的操作的更多信息,请。。。
    namespace WpfDataContextInResources
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = new DataClass()
                    {
                        Text = "Hello",
                    };
            }
        }

        public class TestWrapper : FrameworkElement {}

        public class DataClass
        {
            public string Text { get; set; }
        }
    }
<Window.Resources>
    <FrameworkElement x:Key="DataContextWrapper" />
    <local:TestWrapper x:Key="TestObj" DataContext="{Binding DataContext, Source={StaticResource DataContextWrapper}}" />
    ...
 //of course register this handler!
 void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
 {
        var dcw = this.Resources["DataContextWrapper"] as FrameworkElement;
        dcw.DataContext = this.DataContext;
 }
public class TestWrapper : DependencyObject {}