C# WPF-DataTemplate-在附加属性中使用元素绑定

C# WPF-DataTemplate-在附加属性中使用元素绑定,c#,wpf,xaml,datatemplate,C#,Wpf,Xaml,Datatemplate,我正在开发一个使用格式丰富的列表框的应用程序。我需要做的事情之一是将多条信息绑定到ListBox数据模板中的按钮 这是对我的实际代码的过度简化,我是为了帮助您理解这个问题而编写的 下面是DataTemplate内部的一块XAML: <Button Command="local:MediaCommands.StreamVideo" CommandParameter="{Binding Path=Folder}" /> 但是,代码向我发送的唯一内容是一个空白的FileInfo对

我正在开发一个使用格式丰富的列表框的应用程序。我需要做的事情之一是将多条信息绑定到ListBox数据模板中的按钮

这是对我的实际代码的过度简化,我是为了帮助您理解这个问题而编写的

下面是DataTemplate内部的一块XAML:

<Button Command="local:MediaCommands.StreamVideo"
    CommandParameter="{Binding Path=Folder}" />
但是,代码向我发送的唯一内容是一个空白的
FileInfo
对象。请注意,如果我将上面的XAML更改为包含
文件夹
文件名
的文本值,那么代码工作正常,因为它正确地创建了
文件信息
对象并分配了正确的属性

作为参考,我的
FileInfo
类看起来有点像这样:

class FileInfo : DependencyObject {
    public static readonly DependencyProperty FolderProperty;
    public static readonly DependencyProperty FilenameProperty;
    static FileInfo() {
        FolderProperty = DependencyProperty.Register("Folder",
            typeof(string), typeof(FileInfo));
        FilenameProperty = DependencyProperty.Register("Filename",
            typeof(string), typeof(FileInfo));
    }
    public string Folder {
        get { return (string) GetValue(FolderProperty); }
        set { SetValue(FolderProperty, value); }
    }
    public string Filename {
        get { return (string) GetValue(FilenameProperty); }
        set { SetValue(FilenameProperty, value); }
    }
}

忽略这样一个事实,在本例中,我可以简单地传递对数据对象本身的引用(在我的实际应用程序中,我需要从一对嵌套的
ListBox
s中绘制数据,但问题是相同的),有人能看到这里发生了什么吗?我的依赖项属性是否未正确声明?我需要对绑定做一些奇怪的事情吗?

没有显式声明源的绑定依赖于DataContext作为其源。您尚未在FileInfo实例上声明DataContext,这通常意味着将使用继承的DataContext。DataContext继承既依赖于FrameworkElement,也依赖于运行时可视化树,当您使用分配给未显示在树中的属性的非FrameworkElement派生类时,两者都不起作用。

非常令人费解。帮助我解决绑定问题的一件事是启用Debug->Exceptions->Managed Debug Assistants->BindingFailure下的抛出选项。如果您这样做,调试器可能会告诉您一些有用的信息。我启用了该功能,但它没有报告任何额外内容。一定是别的原因。我尝试使
FileInfo
FrameworkElement
继承,而不是
DependencyObject
,但这并没有解决问题。我是否误读了您的解决方案?John还提到,为了使DataContext继承工作,对象必须是可视化树的一部分。在您的情况下,FileInfo不是。因此,它不会拾取DataContext。无论如何,对于上面的内容,一个可能的解决方案是使用一个IValueConverter,它根据您的数据类返回一个FileInfo实例。在这样做时,您的CommandParameter将被声明为。我看到的问题(可能不是问题)是,我的数据不仅仅来自我的数据类。相反,我需要数据类中的一个字段和数据模板中其他元素(
ListBox
s)中的两个字段。严格地说,我可以不返回任何参数,对按钮的回调进行编程,使其遍历元素树,但这将需要大量工作,而且非常不美观。我觉得应该有更好的解决方案。要从多个对象获取数据,您仍然可以使用@karmicpuppet的建议,但使用多值转换器传递多个绑定值以构建FileInfo对象。
class FileInfo : DependencyObject {
    public static readonly DependencyProperty FolderProperty;
    public static readonly DependencyProperty FilenameProperty;
    static FileInfo() {
        FolderProperty = DependencyProperty.Register("Folder",
            typeof(string), typeof(FileInfo));
        FilenameProperty = DependencyProperty.Register("Filename",
            typeof(string), typeof(FileInfo));
    }
    public string Folder {
        get { return (string) GetValue(FolderProperty); }
        set { SetValue(FolderProperty, value); }
    }
    public string Filename {
        get { return (string) GetValue(FilenameProperty); }
        set { SetValue(FilenameProperty, value); }
    }
}