我正在尝试将我的 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>