C# 分包合同的约束力不';我不能正常工作

C# 分包合同的约束力不';我不能正常工作,c#,wpf,binding,C#,Wpf,Binding,我使用的是C#7、.NET4.7.2和WPF4.5.2 我想显示层次数据。因此,我使用ListBox作为顶级控件,使用ListView作为子级控件。无论使用何种样式,ListBox的DataTemplate如下所示 <DataTemplate> <Border> <Grid> <Grid.RowDefinitions> <RowDefinition Heig

我使用的是C#7、.NET4.7.2和WPF4.5.2

我想显示层次数据。因此,我使用ListBox作为顶级控件,使用ListView作为子级控件。无论使用何种样式,ListBox的DataTemplate如下所示

<DataTemplate>

    <Border>

        <Grid>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"    />
                <RowDefinition Height="Auto"    />
            </Grid.RowDefinitions>


            <TextBlock  Grid.Row="0" Text="{Binding SomeTextProperty}" />
            <ListView   Grid.Row="1" ItemsSource="{Binding SubDataClasses}"
                                     ItemContainerStyle="{DynmicRessource MyListViewItem}" />

        </Grid>

    </Border>

</DataTemplate>
    public ObservableCollection<MyMainDataClass> MyDataSource { get; set; }
<Style x:Key="MyListViewItem" TargetType="{x:Type ListViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <TextBox Text="{Binding SomeValue}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在DataContext中有一个名为MyDataSource的属性,ListBox的ItemsSource将绑定到该属性。物业看起来像这样

<DataTemplate>

    <Border>

        <Grid>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"    />
                <RowDefinition Height="Auto"    />
            </Grid.RowDefinitions>


            <TextBlock  Grid.Row="0" Text="{Binding SomeTextProperty}" />
            <ListView   Grid.Row="1" ItemsSource="{Binding SubDataClasses}"
                                     ItemContainerStyle="{DynmicRessource MyListViewItem}" />

        </Grid>

    </Border>

</DataTemplate>
    public ObservableCollection<MyMainDataClass> MyDataSource { get; set; }
<Style x:Key="MyListViewItem" TargetType="{x:Type ListViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <TextBox Text="{Binding SomeValue}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
公共ObservableCollection MyDataSource{get;set;} 最后但并非最不重要的是数据类:

    public class MySubDataClass
    {
        public string SomeValue { get; set; }
    }


    public class MyMainDataClass
    {
        public string SomeTextProperty { get; set; }

        public ObservableCollection<MySubDataClass> SubDataClasses { get; set; }

    }
public类MySubDataClass
{
公共字符串SomeValue{get;set;}
}
公共类MyMainDataClass
{
公共字符串SomeTextProperty{get;set;}
公共ObservableCollection子数据类{get;set;}
}
列表框中的绑定工作正常。但是SomeTextProperty恰好显示正确。属于每个MyMainDataClass的MySubDataClass的数量也是正确的

但是一些有价值的东西的内容从来没有被展示过。您所能看到的只是类MySubDataClass的名称。通常,在这种情况下会出现绑定错误,例如绑定中属性的名称拼写错误。我的情况不是这样。但一定是出了问题

我用diag:PresentationTraceSources.TraceLevel=High检查了绑定。但是VisualStudio的输出窗口根本没有显示任何内容。没有信息,没有警告,没有错误。什么都没有。ListBox的ItemsSource绑定上的相同跟踪正确地显示了所有内容

我还在测试场景中分别尝试了ListView。工作正常。所以ListView本身并没有什么问题,只是作为ListBox的子控件

有人能帮我吗

提前谢谢

补充:

ListViewItem的样式如下所示

<DataTemplate>

    <Border>

        <Grid>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"    />
                <RowDefinition Height="Auto"    />
            </Grid.RowDefinitions>


            <TextBlock  Grid.Row="0" Text="{Binding SomeTextProperty}" />
            <ListView   Grid.Row="1" ItemsSource="{Binding SubDataClasses}"
                                     ItemContainerStyle="{DynmicRessource MyListViewItem}" />

        </Grid>

    </Border>

</DataTemplate>
    public ObservableCollection<MyMainDataClass> MyDataSource { get; set; }
<Style x:Key="MyListViewItem" TargetType="{x:Type ListViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <TextBox Text="{Binding SomeValue}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我认为ListViewItems的文本框应该绑定MySubDataClass的属性

<Style x:Key="MyListViewItem" TargetType="{x:Type ListViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <TextBox Text="{Binding MySubDataClass.XXXX}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=新的MyMainDataClass();
}
}
公共类MySubDataClass
{
公共字符串SomeValue{get;set;}
}
公共类MyMainDataClass
{
公共字符串SomeTextProperty{get;set;}
公共ObservableCollection子数据类{get;set;}
公共MyMainDataClass()
{
SomeTextProperty=“测试”;
子数据类=新的ObservableCollection();
添加(新的MySubDataClass(){SomeValue=“test1”});
添加(新的MySubDataClass(){SomeValue=“test2”});
}
}

像这样的代码,它工作了。图像是结果。

显然没有用于ListView的ItemTemplate的DataTemplate。您希望如何显示SomeValue属性?请注意,为什么它是ListView(比ListBox更复杂)?是否设置其视图属性?您需要选择支持吗?ItemsControl可能是更好的选择。正如我前面提到的,ListView本身工作良好。因此,ItemContainerStyle设置正确。但确切地说,我在我的原始文章中添加了ListViewItem的样式定义。我确实需要一个选择支持,也可能需要一些类似DataGrid RowDetails的东西。但不是马上。我选择ListView是因为我认为在需要多列的情况下,它可能更容易处理。尽管输入错误
DynmicRessource
,为什么ItemsContainerStyle设置为动态资源?为什么要设置ItemContainerStyle而不是ItemTemplate?更简单的可能是自动选择数据类型设置为MySubDataClass的DataTemplate。在您的
列表视图中
是否尝试使用
DisplayMemberPath
来显示该值?“通常,在这种情况下,存在绑定错误”是不正确的。如果您只看到类名,这意味着您的ItemContainerStyle根本没有被应用,并且ListView使用默认的ItemContainerStyle和ItemTemplate,其中只显示
MySubDataClass.ToString()
的结果。这是错误的。ListViewItem的DataContext是一个MySubDataClass实例,因此
Text=“{Binding SomeValue}”
是正确的。我认为您没有设置其父级的DataContext。请重新阅读问题。OP确实在内部ListView中看到了这些项,只是没有应用它们的ItemContainerStyle。这个答案和你的另一个答案一样错误,两者都应该删除。对不起,这里不是人们猜测答案的地方(比如“我想”)。如果您理解问题并知道解决方案,请只写一个答案。错误就是错误。错误的答案应该被删除。没错。“他们的ItemContainerStyle未应用”的答案是您在ListViewItem的模板中添加了一个文本框。OP就是这样尝试的,但没有成功。对不起,你还不明白问题所在。我正试图理解你的问题,但我真的不知道你想要什么。