绑定到选定的TabItem内容
我有一个带有两个控件的窗口:control1 是一个控件,它有一堆执行 RoutedCommands 的按钮;第二个控件是绑定到文档列表的 TabControl
。 TabControl
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要考虑绑定列表视图,而是考虑绑定到数据。我假设您打算在两个用户控件之间使用数据;在这种情况下,只要 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.