Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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# WPF ItemControl未正确绑定_C#_Wpf_Data Binding - Fatal编程技术网

C# WPF ItemControl未正确绑定

C# WPF ItemControl未正确绑定,c#,wpf,data-binding,C#,Wpf,Data Binding,我有一个绑定到CollectionViewSource的ItemControl。ItemControl项被分组,然后显示在datatemplate中。 我想要实现的是这样的目标: -A-------------- | Aa... | | Aaaa... | ---------------- -B-------------- | Bb... | | Bbb... | ---------------- - -------------- | Aa.

我有一个绑定到CollectionViewSource的ItemControl。ItemControl项被分组,然后显示在datatemplate中。 我想要实现的是这样的目标:

-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列表项;