如何绑定“选定的项目”在 WPF 中的 Listbox 到 ItemsControl 中?

发布于 2024-08-29 02:05:34 字数 3282 浏览 1 评论 0原文

LowDown:我正在尝试在 WPF 中创建一个文档查看器。它将允许用户预览选定的文档,如果需要,还可以比较 WPF 中的文档。这样他们就可以并排查看它们。

布局是这样的:左侧是一个完整的列表框。右侧是 Collection 或 Items 控件。项目控件内部将是列表框中“选定文档”的集合。因此,用户可以在列表框中选择多个项目,并且对于他们选择的每个新项目,他们可以将该项目添加到右侧的集合中。我希望该集合看起来像一个在 Google/Bing 图像搜索中显示的图片库。有道理吗?

我遇到的问题是我无法让 WPFPreviewer 正确绑定到 itemscontrol 下列表框中的所选项目。

旁注: WPFPreviewer 是 Micorosft 推出的,它允许我们预览文档。可以为所有类型的文档构建其他预览器,但我会在这里进行基础操作,直到我使其正常工作为止。

我已经成功地绑定到列表框,而没有此处的项目控件:

<Window.Resources>
    <DataTemplate x:Key="listBoxTemplate">
        <StackPanel Margin="3" >
            <DockPanel >
                <Image Source="{Binding IconURL}" Height="30"></Image>
                <TextBlock Text="  " />
                <TextBlock x:Name="Title" Text="{Binding Title}" 
                    FontWeight="Bold" />
                <TextBlock x:Name="URL" Visibility="Collapsed" 
                    Text="{Binding Url}"/>
            </DockPanel>
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<Grid Background="Cyan">
    <ListBox HorizontalAlignment="Left" 
        ItemTemplate="{StaticResource listBoxTemplate}" Width="200" 
        AllowDrop="True" x:Name="lbDocuments" 
        ItemsSource="{Binding Path=DocumentElements,ElementName=winDocument}"       
        DragEnter="documentListBox_DragEnter"  />
    <l:WPFPreviewHandler
        Content="{Binding ElementName=lbDocuments, Path=SelectedItem.Url}"/>
</Grid>

不过,一旦我添加了 ItemsControl,我就无法再让它工作了:

<Window.Resources>
    <DataTemplate x:Key="listBoxTemplate">
        <StackPanel Margin="3" >
            <DockPanel >
                <Image Source="{Binding IconURL}" Height="30"></Image>
                <TextBlock Text="  " />
                <TextBlock x:Name="Title" Text="{Binding Title}" FontWeight="Bold" />
                <TextBlock x:Name="URL" Visibility="Collapsed" Text="{Binding Url}"/>
            </DockPanel>
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ListBox HorizontalAlignment="Left" 
        ItemTemplate="{StaticResource listBoxTemplate}" Width="200" 
        AllowDrop="True" x:Name="lbDocuments" 
        ItemsSource="{Binding Path=DocumentElements,ElementName=winDocument}"
        DragEnter="documentListBox_DragEnter"  />
    <ItemsControl x:Name="DocumentViewer"  
        ItemsSource="{Binding ElementName=lbDocuments, Path=SelectedItem.Url}" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Background="Cyan">
                    <l:WPFPreviewHandler Content="{Binding Url}"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

这是加载到列表框中的类

    public class Document
{
    public string Title { get; set; } 
    public string Url { get; set; } //this is what I want
    public string IconURL { get; set; } //used so I can display the icon of the file
}

有人可以帮忙吗如果我在列表框中选择一个或什至多个项目,我会尝试绑定到 ItemsControl。

LowDown: I am trying to create a Document Viewer in WPF. It will allow the user to preview selected documents and if they want, compare the documents in WPF. So they can view them side by side.

The layout is this: Left side is a full list box. On the right side is a Collection or an Items control. Inside the items control will be a collection of the "selected documents" in the list box. So A user can select multiple items in the list box and for each new item they select, they can add the item to the collection on the right. I want the collection to look like a image gallery that shows up in Google/Bing Image searches. Make sense?

The problem I am having is I can't get the WPFPreviewer to bind correctly to the selected item in the list box under the itemscontrol.

Side Note: The WPFPreviewer is something Micorosft puts out that allows us to preview documents. Other previewers can be built for all types of documents, but im going basic here until I get this working right.

I have been successful in binding to the list box WITHOUT the items control here:

<Window.Resources>
    <DataTemplate x:Key="listBoxTemplate">
        <StackPanel Margin="3" >
            <DockPanel >
                <Image Source="{Binding IconURL}" Height="30"></Image>
                <TextBlock Text="  " />
                <TextBlock x:Name="Title" Text="{Binding Title}" 
                    FontWeight="Bold" />
                <TextBlock x:Name="URL" Visibility="Collapsed" 
                    Text="{Binding Url}"/>
            </DockPanel>
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<Grid Background="Cyan">
    <ListBox HorizontalAlignment="Left" 
        ItemTemplate="{StaticResource listBoxTemplate}" Width="200" 
        AllowDrop="True" x:Name="lbDocuments" 
        ItemsSource="{Binding Path=DocumentElements,ElementName=winDocument}"       
        DragEnter="documentListBox_DragEnter"  />
    <l:WPFPreviewHandler
        Content="{Binding ElementName=lbDocuments, Path=SelectedItem.Url}"/>
</Grid>

Though, once I add in the ItemsControl, I can't get it to work anymore:

<Window.Resources>
    <DataTemplate x:Key="listBoxTemplate">
        <StackPanel Margin="3" >
            <DockPanel >
                <Image Source="{Binding IconURL}" Height="30"></Image>
                <TextBlock Text="  " />
                <TextBlock x:Name="Title" Text="{Binding Title}" FontWeight="Bold" />
                <TextBlock x:Name="URL" Visibility="Collapsed" Text="{Binding Url}"/>
            </DockPanel>
        </StackPanel>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ListBox HorizontalAlignment="Left" 
        ItemTemplate="{StaticResource listBoxTemplate}" Width="200" 
        AllowDrop="True" x:Name="lbDocuments" 
        ItemsSource="{Binding Path=DocumentElements,ElementName=winDocument}"
        DragEnter="documentListBox_DragEnter"  />
    <ItemsControl x:Name="DocumentViewer"  
        ItemsSource="{Binding ElementName=lbDocuments, Path=SelectedItem.Url}" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Background="Cyan">
                    <l:WPFPreviewHandler Content="{Binding Url}"/>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

Here is the Class of which is loaded into the ListBox

    public class Document
{
    public string Title { get; set; } 
    public string Url { get; set; } //this is what I want
    public string IconURL { get; set; } //used so I can display the icon of the file
}

Can someone please help me out with trying to bind to the ItemsControl if I select one or even multiple items in the listbox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青柠芒果 2024-09-05 02:05:34

看起来错误出在您的 ItemsSource 绑定中。目前,您正在尝试设置 ItemsControl 以显示 SelectedItem 的 Url。相反,您应该只绑定到 SelectedItems (属性名称上的 s 非常重要!)属性:

...
<ItemsControl x:Name="DocumentViewer"  
    ItemsSource="{Binding ElementName=lbDocuments, Path=SelectedItems}" >
...

因为您已经有一个使用 Url 来创建的 DataTemplate PreviewHandler,这应该可以满足您的需要。

编辑
您需要绑定到 SelectedItems,而不是 SelectedItem,因为 SelectedItem 只是一个对象(不是有效的 ItemsSource) ,而 SelectedItems 是所有选定项目的 IEnumerable

It looks like the error is in your ItemsSource binding. Currently, you are trying to set the ItemsControl to display the Url of the SelectedItem. Instead, you should just bind to the SelectedItems (the s on the property name is really important!) property:

...
<ItemsControl x:Name="DocumentViewer"  
    ItemsSource="{Binding ElementName=lbDocuments, Path=SelectedItems}" >
...

Since you already have a DataTemplate that is using the Url to create the PreviewHandler, this should do what you need.

Edit
You need to bind to SelectedItems, and not SelectedItem because SelectedItem is only one object (not a valid ItemsSource), whereas SelectedItems is an IEnumerable of all the items selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文