绑定到选定的TabItem内容

发布于 2024-08-03 22:41:13 字数 3488 浏览 5 评论 0原文

我有一个带有两个控件的窗口:control1 是一个控件,它有一堆执行 RoutedCommands 的按钮;第二个控件是绑定到文档列表的 TabControlTabControl ContentTemplate 是一个用户控件 control2,它具有一个 ListView。我想将 control1 的 DataContext 绑定到所选选项卡的 control2 的 ListView 。代码如下:

<Window x:Class="deleteme.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:deleteme"
    xmlns:sc="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <sc:ArrayList x:Key="Tabs">
            <local:Document Name="doc1" />
            <local:Document Name="doc2" />
            <local:Document Name="doc3" />
        </sc:ArrayList>
    </Window.Resources>
    <DockPanel>
        <local:control1 DockPanel.Dock="Left" />
        <TabControl DockPanel.Dock="Left" ItemsSource="{StaticResource Tabs}" SelectionChanged="TabControl_SelectionChanged">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <local:control2 />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </DockPanel>
</Window>

<UserControl x:Class="deleteme.control1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:deleteme"
    x:Name="control">
    <StackPanel>
        <ItemsControl ItemsSource="{DynamicResource Actions}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button x:Name="actionButton" Content="{Binding Path=Text}" IsEnabled="{Binding Path=actionButton_IsEnabled}" Command="{Binding Path=Command}" CommandTarget="{Binding ElementName=control, Path=DataContext}" Click="actionButton_Click" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</UserControl>

<UserControl x:Class="deleteme.control2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <ScrollViewer>
            <ListView x:Name="listView" ItemsSource="{Binding Items}" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ScrollViewer>
    </Grid>
</UserControl>

我想要将 control2 的 ListView 绑定到 control1 的原因有两个:我可以使用 ListView.SelectedItem 作为 RoatedCommand 的参数。执行,我可以使用ListView 作为CommandTarget,以便我可以处理与control2 中的文档相关的操作。

我的问题是我无法弄清楚如何将 control2 的 ListView 绑定到 control1。实际上,由于我是 WPF 新手,因此更有可能我错误地处理了这个问题。如果有人有建议,我将不胜感激。

I have a Window with two controls: control1 is a control that has a bunch of buttons that execute RoutedCommands; the second control is a TabControl that is bound to a list of documents. The TabControl ContentTemplate is a user control, control2, that has a ListView. I would like to bind the DataContext of control1 to the ListView of control2 of the selected tab. Here is the code:

<Window x:Class="deleteme.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:deleteme"
    xmlns:sc="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <sc:ArrayList x:Key="Tabs">
            <local:Document Name="doc1" />
            <local:Document Name="doc2" />
            <local:Document Name="doc3" />
        </sc:ArrayList>
    </Window.Resources>
    <DockPanel>
        <local:control1 DockPanel.Dock="Left" />
        <TabControl DockPanel.Dock="Left" ItemsSource="{StaticResource Tabs}" SelectionChanged="TabControl_SelectionChanged">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <local:control2 />
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </DockPanel>
</Window>

<UserControl x:Class="deleteme.control1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:deleteme"
    x:Name="control">
    <StackPanel>
        <ItemsControl ItemsSource="{DynamicResource Actions}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button x:Name="actionButton" Content="{Binding Path=Text}" IsEnabled="{Binding Path=actionButton_IsEnabled}" Command="{Binding Path=Command}" CommandTarget="{Binding ElementName=control, Path=DataContext}" Click="actionButton_Click" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</UserControl>

<UserControl x:Class="deleteme.control2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <ScrollViewer>
            <ListView x:Name="listView" ItemsSource="{Binding Items}" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ScrollViewer>
    </Grid>
</UserControl>

The reason that I want to bind the ListView of control2 to control1 is twofold: I can use ListView.SelectedItem as the parameter to RoutedCommand.Execute, and I can use the ListView as the CommandTarget so that I can handle actions that pertain to a document in control2.

My problem is that I cannot figure out how to bind the ListView of control2 to control1. Actually, its more likely that I am approaching the problem incorrectly as I'm new to WPF. If anyone has a suggestion, I would appreciate it.

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

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

发布评论

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

评论(1

〆一缕阳光ご 2024-08-10 22:41:13

不要考虑绑定列表视图,而是考虑绑定到数据。我假设您打算在两个用户控件之间使用数据;在这种情况下,只要 DataContext 位于树的较高位置,那么两个控件都可以访问它。

Don't think in terms of binding the listview, think in terms of binding to the data instead. I assume that you are intending the data to be used between the two user controls; in which case, as long as you have the DataContext higher up the tree then both controls can get access to it.

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