在 Silverlight 中构建动态 TabControl
我想在 Silverlight 中构建一个由对象集合驱动的 TabControl。我将展示下面的代码,其中包含我正在尝试制作原型的非常基本的设置。
MainPage.xaml
<UserControl x:Class="DataDrivenTabControl.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:local="clr-namespace:DataDrivenTabControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.DataContext>
<local:MainPage_ViewModel/>
</UserControl.DataContext>
<StackPanel>
<controls:TabControl>
<!-- What do I need to do here for a Template? -->
</controls:TabControl>
</StackPanel>
MainPage_ViewModel.cs
public class MainPage_ViewModel : Base_ViewModel
{
public MainPage_ViewModel()
{
PopulateCollectionOfTabs();
}
public ObservableCollection<TabItem_DataViewModel> CollectionOfTabs
{
get { return collectionOfTabs; }
set
{
if (collectionOfTabs != value)
{
collectionOfTabs = value;
OnPropertyChanged("CollectionOfTabs");
}
}
}
private ObservableCollection<TabItem_DataViewModel> collectionOfTabs = new ObservableCollection<TabItem_DataViewModel>();
private void PopulateCollectionOfTabs()
{
this.CollectionOfTabs.Add(
new TabItem_DataViewModel()
{
TabDescription = "Tab 1",
ButtonDescription = "Button for Tab 1"
});
this.CollectionOfTabs.Add(
new TabItem_DataViewModel()
{
TabDescription = "Tab 2",
ButtonDescription = "Button for Tab 2"
});
}
}
TabItem_DataViewModel.cs
public class TabItem_DataViewModel : Base_ViewModel
{
public string TabDescription
{
get { return tabDescription; }
set
{
if (tabDescription != value)
{
tabDescription = value;
OnPropertyChanged("TabDescription");
}
}
}
private string tabDescription = string.Empty;
public string ButtonDescription
{
get { return buttonDescription; }
set
{
if (buttonDescription != value)
{
buttonDescription = value;
OnPropertyChanged("ButtonDescription");
}
}
}
private string buttonDescription = string.Empty;
}
在这个示例中我真正想做的就是让 TabControl 显示一个动态选项卡列表,这些选项卡将绑定到一个集合(“Tab 1”和“Tab”) 2”在当前实现的标题中)。当您单击选项卡时,可能会出现一个按钮(为了简单起见),该按钮的内容将绑定到 TabItem_DataViewModel 上的 ButtonDescription。这是非常基本的,但如果我能让它发挥作用,我肯定能够实现我的解决方案的其余部分。
我确信这必须使用 TabControl 上的模板来完成,但我只是将其留空,希望有人能够为我指明正确的方向。
任何帮助将不胜感激,谢谢!
I would like to build a TabControl in Silverlight which is driven by a collection of objects. I'll show the code below of a VERY basic setup that I'm trying to prototype.
MainPage.xaml
<UserControl x:Class="DataDrivenTabControl.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:local="clr-namespace:DataDrivenTabControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.DataContext>
<local:MainPage_ViewModel/>
</UserControl.DataContext>
<StackPanel>
<controls:TabControl>
<!-- What do I need to do here for a Template? -->
</controls:TabControl>
</StackPanel>
MainPage_ViewModel.cs
public class MainPage_ViewModel : Base_ViewModel
{
public MainPage_ViewModel()
{
PopulateCollectionOfTabs();
}
public ObservableCollection<TabItem_DataViewModel> CollectionOfTabs
{
get { return collectionOfTabs; }
set
{
if (collectionOfTabs != value)
{
collectionOfTabs = value;
OnPropertyChanged("CollectionOfTabs");
}
}
}
private ObservableCollection<TabItem_DataViewModel> collectionOfTabs = new ObservableCollection<TabItem_DataViewModel>();
private void PopulateCollectionOfTabs()
{
this.CollectionOfTabs.Add(
new TabItem_DataViewModel()
{
TabDescription = "Tab 1",
ButtonDescription = "Button for Tab 1"
});
this.CollectionOfTabs.Add(
new TabItem_DataViewModel()
{
TabDescription = "Tab 2",
ButtonDescription = "Button for Tab 2"
});
}
}
TabItem_DataViewModel.cs
public class TabItem_DataViewModel : Base_ViewModel
{
public string TabDescription
{
get { return tabDescription; }
set
{
if (tabDescription != value)
{
tabDescription = value;
OnPropertyChanged("TabDescription");
}
}
}
private string tabDescription = string.Empty;
public string ButtonDescription
{
get { return buttonDescription; }
set
{
if (buttonDescription != value)
{
buttonDescription = value;
OnPropertyChanged("ButtonDescription");
}
}
}
private string buttonDescription = string.Empty;
}
All I'm really trying to do in this example is to get the TabControl to show up with a dynamic list of tabs which would be bound to a collection ("Tab 1" & "Tab 2" in the header for the current implementation). When you click on a Tab, there could be a button (for simplicity), where the content of the button would bind to ButtonDescription on the TabItem_DataViewModel. This is very basic, but if I can get this to work, I'll for sure be able to implement the rest of my solution.
I'm sure this has to be done with templates on the TabControl, but I just left it empty hoping that someone would be able to point me in the right direction.
Any help will be greatly appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用 Telerik 的 RadTabControl,我能够让它按预期工作,如下所示。我使用 Telerik 的版本,因为 ItemSource 是 IEnumerable,而不是 TabItem 的集合。
I was able to get it to work as expected by using Telerik's RadTabControl as shown below. I used Telerik's version since the ItemSource is an IEnumerable, not a collection of TabItems.