在 Silverlight 中构建动态 TabControl

发布于 2024-09-11 04:15:13 字数 3127 浏览 1 评论 0原文

我想在 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 技术交流群。

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

发布评论

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

评论(1

悟红尘 2024-09-18 04:15:13

通过使用 Telerik 的 RadTabControl,我能够让它按预期工作,如下所示。我使用 Telerik 的版本,因为 ItemSource 是 IEnumerable,而不是 TabItem 的集合。

<telerikNavigation:RadTabControl ItemsSource="{Binding CollectionOfTabs}">
        <telerikNavigation:RadTabControl.ItemContainerStyle>
            <Style TargetType="telerikNavigation:RadTabItem">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding TabDescription}"/>
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Button Content="{Binding ButtonDescription}" Width="100" HorizontalAlignment="Center"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </telerikNavigation:RadTabControl.ItemContainerStyle>
    </telerikNavigation:RadTabControl>

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.

<telerikNavigation:RadTabControl ItemsSource="{Binding CollectionOfTabs}">
        <telerikNavigation:RadTabControl.ItemContainerStyle>
            <Style TargetType="telerikNavigation:RadTabItem">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding TabDescription}"/>
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Button Content="{Binding ButtonDescription}" Width="100" HorizontalAlignment="Center"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </telerikNavigation:RadTabControl.ItemContainerStyle>
    </telerikNavigation:RadTabControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文