Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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
C# 将数据模板元素绑定到子类上的属性_C#_Wpf_Xaml_Binding_Datatemplate - Fatal编程技术网

C# 将数据模板元素绑定到子类上的属性

C# 将数据模板元素绑定到子类上的属性,c#,wpf,xaml,binding,datatemplate,C#,Wpf,Xaml,Binding,Datatemplate,为了实验起见,我有一个班叫foo,另一个班叫bar 我在xaml中为类foo定义了一个数据模板,但foo的一个属性是bar对象,这样 foo() { Public string Name {get; set;} Public int ID {get; set;} Public bar barProp {get; set;} } 及 我希望我的foo数据模板显示bar的Description属性。 我试过简单的和变体,但都没有用 寻求智慧,, DJ 编辑: 根据对更多信息

为了实验起见,我有一个班叫foo,另一个班叫bar 我在xaml中为类foo定义了一个数据模板,但foo的一个属性是bar对象,这样

foo()
{
    Public string Name {get; set;}
    Public int ID {get; set;}
    Public bar barProp {get; set;}
}

我希望我的foo数据模板显示bar的Description属性。 我试过简单的和变体,但都没有用

寻求智慧,, DJ

编辑: 根据对更多信息的要求。。。 这是我真正的课程

public class AccountRecord
{
    public string Value { get; set; }
    public string Identifier { get; set; }
    public Field AccountNumber;
}
public class Field
{
    public string Name{get;set;}
    public string Value{get;set}
}
这里是用于模板的XAML

<ListBox Margin="0,35,0,0" Name="listAccountRecords" Background="Transparent" BorderBrush="Transparent" ItemsSource="{Binding AccountRecords, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
    <ListBox.ItemTemplate>
            <DataTemplate DataType="{x:Type lib:AccountRecord}">
                <Expander Header="{Binding AccountNumber.Name}">                               
                    <ListView ItemsSource="{Binding Fields}" ItemTemplate="{DynamicResource FieldTemplate}">
                    </ListView>
                </Expander>
            </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
确切的问题是AccountNumber.Name值没有显示在AccountRecord元素的expander标头中

<textblock Text="{Binding Path=FooObjectName.barProp.Description}" />
希望这能奏效。。 祝你好运

试试这个

<textblock Text="{Binding Path=FooObjectName.barProp.Description}" />
希望这能奏效。。
祝你好运

字段类型的AccountNumber成员只是一个字段,而不是属性。只能绑定到属性。给它一个getter和setter,它就会开始工作。

类型为Field的AccountNumber成员只是一个字段,而不是属性。只能绑定到属性。给它一个getter和setter,它就会开始工作。

应该可以。WPF的绑定系统完全支持嵌套属性。你看到了什么行为?您可能需要发布一个更具体的示例。检查TextBlock.DataContext属性-验证它是否设置为您的Foo类型实例。绑定似乎是正确的。我已经添加了我正在处理的确切案例,应该可以正常工作。WPF的绑定系统完全支持嵌套属性。你看到了什么行为?您可能需要发布一个更具体的示例。检查TextBlock.DataContext属性-验证它是否设置为您的Foo类型实例。绑定似乎是正确的。我已经添加了我正在处理的确切案例谢谢你的评论:我已经尝试过了,当它不起作用时我感到困惑,所以我认为有更险恶的东西在起作用。有一个更完整的例子可供你阅读:谢谢你的评论:我已经试过了,当它不起作用时我感到困惑,所以我认为有更邪恶的东西在起作用。有一个更完整的示例可供您阅读:@Terror这绝对是WPF绑定的常见问题。它需要以某种方式更清晰地记录下来。@当涉及到WPF绑定时,这绝对是一个常见问题。它需要以某种方式更明显地记录下来。