我是wpf的初学者。我将从两个选项卡中获得许多按钮。应该是5。但是,我只能从第一个标签中获得两个按钮。
我试图切换到tab2,然后找不到按钮。
您知道如何从所有选项卡中获取所有控件吗?我将事件绑定到每个选项卡中的所有画布和按钮。
这是我的MainWindow:
<TabControl Name="tabMain" ItemsSource="{Binding Tabs}">
<TabControl.ItemTemplate>
<!-- this is the header template-->
<DataTemplate>
<TextBlock
Text="{Binding Header}" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate DataType="{x:Type local:MyTabItem}">
<ItemsControl ItemsSource="{Binding Content}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:MyTabItemCtrl}" >
<Button Content="{Binding Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=PosX}" />
<Setter Property="Canvas.Top" Value="{Binding Path=PosY}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
MainWindow.xaml.cs:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var btns = UIHelper.FindVisualChildren<Button>(tabMain).ToList();
// btns == 2
}
ViewModels
public sealed class MyViewModel
{
public ObservableCollection<MyTabItem> Tabs { get; set; }
public MyViewModel()
{
Tabs = new ObservableCollection<MyTabItem>();
ObservableCollection<MyTabItemCtrl> ct1 = new ObservableCollection<MyTabItemCtrl>();
ct1.Add(new MyTabItemCtrl { Name = "aaa", Age = 5, PosX=10, PosY=10 });
ct1.Add(new MyTabItemCtrl { Name = "bbb", Age = 5, PosX = 50, PosY = 10 });
ObservableCollection<MyTabItemCtrl> ct2 = new ObservableCollection<MyTabItemCtrl>();
ct2.Add(new MyTabItemCtrl { Name = "aaa", Age = 6, PosX = 10, PosY = 10 });
ct2.Add(new MyTabItemCtrl { Name = "bbb", Age = 6, PosX = 55, PosY = 10 });
ct2.Add(new MyTabItemCtrl { Name = "ccc", Age = 6, PosX = 100, PosY = 10 });
Tabs.Add(new MyTabItem { Header = "One", Content = ct1 });
Tabs.Add(new MyTabItem { Header = "Two", Content = ct2 });
}
}
public sealed class MyTabItem
{
public string Header { get; set; }
public ObservableCollection<MyTabItemCtrl> Content { get; set; }
}
public sealed class MyTabItemCtrl
{
public string Name { get; set; }
public int Age { get; set; }
public double PosX { get; set; }
public double PosY { get; set; }
}
答案 0 :(得分:0)
仅将当前可见的TabItem
加载到可视树中,因此您将无法使用UIHelper.FindVisualChildren
方法来查找当前不可见的任何元素。
当您离开选项卡时,其内容将从可视树中卸载。因此,当选择另一个标签时,这些Buttons
实际上并不存在。
ContentTemplate
只是一个模板。必须在创建任何实际的Button
元素并将其添加到可视树之前将其实现。