【问题标题】:Binding window items to resource properties将窗口项绑定到资源属性
【发布时间】:2012-01-23 13:12:59
【问题描述】:

我有一个 WPF 测试窗口,在该窗口中我声明了一个窗口资源,并且我想将窗口的数据上下文绑定到该资源对象上的依赖属性。这是我的例子:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"

        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
        >

    <Window.Resources>
        <local:TestObj x:Key="MyResObj" Target="{Binding DataContext, diagnostics:PresentationTraceSources.TraceLevel=High}" />
    </Window.Resources> 

    <Grid>
        <TextBox Tag="{StaticResource MyResObj}" />
    </Grid>
</Window>

以及后面的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.DataContext = this;

        InitializeComponent();
    }
}

这是我的TestObj:

namespace WpfApplication1
{
    public class TestObj : DependencyObject
    {

        public static readonly DependencyProperty TargetProperty = DependencyProperty.Register("Target", typeof(object), typeof(TestObj));

        public object Target
        {
            get { return this.GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

    }
}

这是一个简化的示例,我希望最终将窗口中更复杂的内容绑定到 Window.Resources 部分中声明的项目。目前这没有像我预期的那样工作,所以我启用了详细绑定日志记录 - 我得到的绑定错误是

System.Windows.Data 警告:54:为绑定 (hash=40782967) 创建了 BindingExpression (hash=38018250)
System.Windows.Data 警告:56:路径:“DataContext”
System.Windows.Data 警告:58:BindingExpression (hash=38018250):默认模式解析为 OneWay
System.Windows.Data 警告:59:BindingExpression (hash=38018250):默认更新触发器解析为 PropertyChanged
System.Windows.Data 警告:60:BindingExpression (hash=38018250):附加到 WpfApplication16.TestObj.Target (hash=39268741)
System.Windows.Data 警告:62:BindingExpression (hash=38018250):使用框架指导
System.Windows.Data 警告:65:BindingExpression (hash=38018250):正在解析源
System.Windows.Data 警告:67:BindingExpression (hash=38018250):找不到框架导师
System.Windows.Data 警告:63:BindingExpression (hash=38018250):解决源延迟
'WpfApplication16.vshost.exe'(托管 (v4.0.30319)):已加载 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll' System.Windows.Data 警告:65:BindingExpression (hash=38018250):正在解析源
System.Windows.Data 警告:67:BindingExpression (hash=38018250):找不到框架导师
System.Windows.Data 警告:65:BindingExpression (hash=38018250):解析源(最后机会)
System.Windows.Data 警告:67:BindingExpression (hash=38018250):找不到框架导师
System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。绑定表达式:路径=数据上下文;数据项=空;目标元素是“TestObj”(HashCode=39268741);目标属性是“目标”(类型“对象”)

甚至可以将窗口项绑定到这样的资源吗?还是我的装订有误?

谢谢!!

【问题讨论】:

    标签: c# wpf data-binding resources


    【解决方案1】:

    我不认为你可以在它自己的资源中绑定到一个根对象(但不要引用我的话)。您可以尝试将子网格绑定到它吗?

    <Grid>
        <Grid.Resources>
            <local:TestObj x:Key="MyResObj" Target="{Binding DataContext, diagnostics:PresentationTraceSources.TraceLevel=High}" />
        </Grid.Resources> 
        <TextBox Tag="{StaticResource MyResObj}" />
    </Grid>
    

    我不确定资源是否知道逻辑树:/

    【讨论】:

    • 不幸的是,这也不起作用,它具有相同的绑定错误。最终 TestObj 将是一个 ICommand 对象,不在可视树中的项目(如上下文菜单项)可以使用,这就是我尝试将窗口项目绑定到它的原因。
    • 为什么不能直接将 Tag 设置为 DataContext,然后从上下文菜单中访问它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多