C# WPF ItemControl未正确绑定
我有一个绑定到CollectionViewSource的ItemControl。ItemControl项被分组,然后显示在datatemplate中。 我想要实现的是这样的目标:C# WPF ItemControl未正确绑定,c#,wpf,data-binding,C#,Wpf,Data Binding,我有一个绑定到CollectionViewSource的ItemControl。ItemControl项被分组,然后显示在datatemplate中。 我想要实现的是这样的目标: -A-------------- | Aa... | | Aaaa... | ---------------- -B-------------- | Bb... | | Bbb... | ---------------- - -------------- | Aa.
-A--------------
| Aa... |
| Aaaa... |
----------------
-B--------------
| Bb... |
| Bbb... |
----------------
- --------------
| Aa... |
| Aaaa... |
----------------
- --------------
| Bb... |
| Bbb... |
----------------
<GroupBox>
<GroupBox.Header>
<TextBlock Text="{Binding initial}"/>
</GroupBox.Header>
<ItemsPresenter />
</GroupBox>
<GroupBox Header="{Binding Name}">
<ItemsPresenter />
</GroupBox>
这是我的密码:
XAML
<CollectionViewSource x:Key="itembyAlpha" Source="{Binding listItem}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="initial" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<ItemsControl ItemsSource="{Binding Source={StaticResource itembyAlpha}}">
<!--GroupStyle-->
<ItemsControl.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<GroupBox Header="{Binding initial}">
<ItemsPresenter />
</GroupBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ItemsControl.GroupStyle>
<!--Item Template-->
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding title}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
GroupBox的标题为空。看来装订不好。
谁能帮我
之前谢谢。像这样试试:
-A--------------
| Aa... |
| Aaaa... |
----------------
-B--------------
| Bb... |
| Bbb... |
----------------
- --------------
| Aa... |
| Aaaa... |
----------------
- --------------
| Bb... |
| Bbb... |
----------------
<GroupBox>
<GroupBox.Header>
<TextBlock Text="{Binding initial}"/>
</GroupBox.Header>
<ItemsPresenter />
</GroupBox>
<GroupBox Header="{Binding Name}">
<ItemsPresenter />
</GroupBox>
或
像这样尝试:
-A--------------
| Aa... |
| Aaaa... |
----------------
-B--------------
| Bb... |
| Bbb... |
----------------
- --------------
| Aa... |
| Aaaa... |
----------------
- --------------
| Bb... |
| Bbb... |
----------------
<GroupBox>
<GroupBox.Header>
<TextBlock Text="{Binding initial}"/>
</GroupBox.Header>
<ItemsPresenter />
</GroupBox>
<GroupBox Header="{Binding Name}">
<ItemsPresenter />
</GroupBox>
或
这是因为您绑定到了错误的字段。您需要绑定到而不是绑定到分组依据的字段。试着这样想:
-A--------------
| Aa... |
| Aaaa... |
----------------
-B--------------
| Bb... |
| Bbb... |
----------------
- --------------
| Aa... |
| Aaaa... |
----------------
- --------------
| Bb... |
| Bbb... |
----------------
<GroupBox>
<GroupBox.Header>
<TextBlock Text="{Binding initial}"/>
</GroupBox.Header>
<ItemsPresenter />
</GroupBox>
<GroupBox Header="{Binding Name}">
<ItemsPresenter />
</GroupBox>
每个组都是一个组,它有自己的属性,您可以在指定组标题时使用这些属性。这是因为您绑定到了错误的字段。您需要绑定到而不是绑定到分组依据的字段。试着这样想:
-A--------------
| Aa... |
| Aaaa... |
----------------
-B--------------
| Bb... |
| Bbb... |
----------------
- --------------
| Aa... |
| Aaaa... |
----------------
- --------------
| Bb... |
| Bbb... |
----------------
<GroupBox>
<GroupBox.Header>
<TextBlock Text="{Binding initial}"/>
</GroupBox.Header>
<ItemsPresenter />
</GroupBox>
<GroupBox Header="{Binding Name}">
<ItemsPresenter />
</GroupBox>
每个组都是一个组,它有自己的属性,您可以在指定组头时使用这些属性。要使绑定像这样工作,应该实现接口INotifyCollectionChanged。 我建议你使用一个列表而不是一个列表 所以这个代码:
List<Movie> lst;
public List<Movie> listItem
{
get { return lst; }
set { lst = value; }
}
列表lst;
公共列表项
{
获取{return lst;}
设置{lst=value;}
}
将成为:
ObservableCollection<Movie> listItem;
ObservableCollection列表项;
要使绑定像这样工作,应实现接口INotifyCollectionChanged。
我建议你使用一个列表而不是一个列表
所以这个代码:
List<Movie> lst;
public List<Movie> listItem
{
get { return lst; }
set { lst = value; }
}
列表lst;
公共列表项
{
获取{return lst;}
设置{lst=value;}
}
将成为:
ObservableCollection<Movie> listItem;
ObservableCollection列表项;