0

我正在尝试将我的 ListView 数据绑定设置为 ObservableCollection。我添加了上下文菜单,并检查列表视图中每个项目的权限。所以我收到一个错误:“在使用 ItemsSource 之前,项目集合必须为空”。但是如果我删除 ListView 中的 ItemsSource,我就不能使用数据绑定。你能帮助我吗?您可以在http://www.mediafire.com/?r21lm8ud9q92o5k下载示例

        <ListView ItemsSource="{Binding}"
        Height="259" HorizontalAlignment="Left" Margin="12,26,0,0" Name="listView1" VerticalAlignment="Top" Width="479">
        <ListView.Items>                
            <ListViewItem>                    
                <ListViewItem.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Kiểm tra">
                            <MenuItem.Icon>
                                <Image Source="Images/CompareFiles.png" Style="{StaticResource ImageContextMenu}"/>
                            </MenuItem.Icon>
                        </MenuItem>

                        <MenuItem Header="Download" Visibility="{Binding Path=Name, Converter={StaticResource MenuContext}, ConverterParameter= {x:Static ObservableCollectionProblem:EnumUserAction.Download}}">
                            <MenuItem.Icon>
                                <Image Source="Images/SyncCurrentObject.png" Style="{StaticResource ImageContextMenu}"/>
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Upload" Visibility="{Binding Path=Actions, Converter={StaticResource MenuContext}, ConverterParameter = {x:Static ObservableCollectionProblem:EnumUserAction.Upload}}">
                            <MenuItem.Icon>
                                <Image Source="Images/upload.png" Style="{StaticResource ImageContextMenu}"/>
                            </MenuItem.Icon>
                        </MenuItem>
                    </ContextMenu>
                </ListViewItem.ContextMenu>
                <GridView>
                    <GridViewColumn Width="250" DisplayMemberBinding="{Binding Path=Name}" Header="Name"></GridViewColumn>
                    <GridViewColumn Width="250" DisplayMemberBinding="{Binding Path=Actions, Converter={StaticResource ActionToStringConverter}}" Header="Action"></GridViewColumn>
                </GridView>
            </ListViewItem>
        </ListView.Items>
        <ListView.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Kiểm tra">
                    <MenuItem.Icon>
                        <Image Source="Images/CompareFiles.png" Style="{StaticResource ImageContextMenu}"/>
                    </MenuItem.Icon>
                </MenuItem>

                <MenuItem Header="Download" Visibility="{Binding Path=Name, Converter={StaticResource MenuContext}, ConverterParameter= {x:Static ObservableCollectionProblem:EnumUserAction.Download}}">
                    <MenuItem.Icon>
                        <Image Source="Images/SyncCurrentObject.png" Style="{StaticResource ImageContextMenu}"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Upload" Visibility="{Binding Path=Actions, Converter={StaticResource MenuContext}, ConverterParameter = {x:Static ObservableCollectionProblem:EnumUserAction.Upload}}">
                    <MenuItem.Icon>
                        <Image Source="Images/upload.png" Style="{StaticResource ImageContextMenu}"/>
                    </MenuItem.Icon>
                </MenuItem>                    
            </ContextMenu>
        </ListView.ContextMenu>
        <ListView.View>
            <GridView>                    
                <GridViewColumn Width="250" DisplayMemberBinding="{Binding Path=Name}" Header="Name"></GridViewColumn>
                <GridViewColumn Width="250" DisplayMemberBinding="{Binding Path=Actions, Converter={StaticResource ActionToStringConverter}}" Header="Action"></GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
4

2 回答 2

0

我在 ItemContainerStyle 中添加上下文菜单及其成功

<ListView Height="259" HorizontalAlignment="Left" Margin="12,26,0,0" Name="listView1" VerticalAlignment="Top" Width="479">                         
        <ListView.Resources>
            <ContextMenu x:Key="ItemContextMenu">                    
                <MenuItem Header="Kiểm tra">
                    <MenuItem.Icon>
                        <Image Source="Images/CompareFiles.png" Style="{StaticResource ImageContextMenu}"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Download" Visibility="{Binding Path=Actions, Converter={StaticResource MenuContext}, ConverterParameter= {x:Static ObservableCollectionProblem:EnumUserAction.Download}}">
                    <MenuItem.Icon>
                        <Image Source="Images/SyncCurrentObject.png" Style="{StaticResource ImageContextMenu}"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Upload" Visibility="{Binding Path=Actions, Converter={StaticResource MenuContext}, ConverterParameter = {x:Static ObservableCollectionProblem:EnumUserAction.Upload}}">
                    <MenuItem.Icon>
                        <Image Source="Images/upload.png" Style="{StaticResource ImageContextMenu}"/>
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </ListView.Resources>
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnListViewItem_PreviewMouseLeftButtonDown" />
                <Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>                
            <GridView>                    
                <GridViewColumn Width="250" DisplayMemberBinding="{Binding Path=Name}" Header="Name"></GridViewColumn>
                <GridViewColumn Width="250" DisplayMemberBinding="{Binding Path=Actions, Converter={StaticResource ActionToStringConverter}}" Header="Action"></GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
于 2012-06-01T02:19:16.527 回答
0

您只能为 a 中的项目提供一次来源ListView,在这种情况下,您似乎ListView.Items在 XAML 中显式设置集合。这将导致您在尝试使用 ItemsSource 绑定时看到的错误。

也许尝试ListView.Items从 XAML 中删除节点并仅使用ItemsSource绑定来填充列表。

看起来您还想使用ListView.ItemTemplate来格式化您想要的项目并获得正确的上下文菜单。

于 2012-05-31T14:30:01.493 回答