WPF项控件数据绑定不工作
我有以下类结构,所有类都实现了WPF项控件数据绑定不工作,wpf,Wpf,我有以下类结构,所有类都实现了INotifyPropertyChanged class University { List<Faculty> Faculties; } class Faculty { string Name; List<Degree> Degrees; bool IsSelected } class Degree { string Code; List<Subject> Subjects
INotifyPropertyChanged
class University
{
List<Faculty> Faculties;
}
class Faculty
{
string Name;
List<Degree> Degrees;
bool IsSelected
}
class Degree
{
string Code;
List<Subject> Subjects
bool IsSelected
}
class Subject
{
string Nme;
string Code;
bool IsSelected
}
这是行不通的。错误是什么???您不能将当前功能用作第一个itemscontrol的itemssource,因为它只是一个对象,而不是集合。我不知道你为什么要这么做 如果要将父项控件替换为ContentControl,则只要在视图模型中正确设置了CurrentFaculty,并且视图datacontext正确设置为视图模型,该控件就可以正常工作
<ContentControl Grid.Column="2" Grid.Row="0" Content="{Binding CurrentFaculty, Mode=TwoWay}">
<ContentControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<CheckBox Margin="22,10,0,0" IsChecked="{Binding IsSelected, Mode=TwoWay}">
<TextBlock Text="All"/>
</CheckBox>
<ItemsControl Grid.Column="2" Grid.Row="0" ItemsSource="{Binding Degrees, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox Margin="22,10,0,0" IsChecked="{Binding IsSelected, Mode=TwoWay}">
<TextBlock Text="{Binding Name, Mode=TwoWay}"/>
</CheckBox>
<ItemsControl ItemsSource="{Binding Subjects, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Margin="42,10,0,0" IsChecked="{Binding IsSelected, Mode=TwoWay}">
<TextBlock Text="{Binding Name}"></TextBlock>
</CheckBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
这应该行得通,我不能保证它显示的内容看起来会很好,但这取决于您。您是否检查了输出窗口中的任何绑定错误?
<ContentControl Grid.Column="2" Grid.Row="0" Content="{Binding CurrentFaculty, Mode=TwoWay}">
<ContentControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<CheckBox Margin="22,10,0,0" IsChecked="{Binding IsSelected, Mode=TwoWay}">
<TextBlock Text="All"/>
</CheckBox>
<ItemsControl Grid.Column="2" Grid.Row="0" ItemsSource="{Binding Degrees, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<CheckBox Margin="22,10,0,0" IsChecked="{Binding IsSelected, Mode=TwoWay}">
<TextBlock Text="{Binding Name, Mode=TwoWay}"/>
</CheckBox>
<ItemsControl ItemsSource="{Binding Subjects, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Margin="42,10,0,0" IsChecked="{Binding IsSelected, Mode=TwoWay}">
<TextBlock Text="{Binding Name}"></TextBlock>
</CheckBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>