根据属性切换TabControl模板

问题描述 投票:1回答:1

我想切换TabControl的ItemTemplate和ContentTemplate。因此,我为TabItem创建了两个DataTemplates,为TabContent创建了两个DataTemplates。如果CcLoginViewModel中的IsRunningSession属性为true,则模板应该切换。我的问题是如何在DataTrigger中访问此属性。

Xaml:

    <DataTemplate x:Key="LoginContentDataTemplate" x:Name="LoginTemplate" DataType="{x:Type vm:CcPlayerViewModel}">
        <local:CcLoginControl x:Name="LoginContentTemplate" DataContext="{Binding Login}" />
    </DataTemplate>
    <DataTemplate x:Key="PlayerContentDataTemplate" DataType="{x:Type vm:CcPlayerViewModel}">
        <local:CcPlayerControl x:Name="PlayerContentDataTemplate" DataContext="{Binding}" />
    </DataTemplate>

    <Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
        <Setter Property="ContentTemplate" Value="{StaticResource LoginContentDataTemplate}" />
        <Setter Property="ItemTemplate" Value="{StaticResource TabControlNewItemDataTemplate}" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding Login.IsRunningSession, Source={StaticResource PlayerTabItemTemplate}}" Value="True">  <-- Binding?
                <Setter Property="ContentTemplate" Value="{StaticResource PlayerContentDataTemplate}" />
                <Setter Property="ItemTemplate" Value="{StaticResource PlayerTabItemTemplate}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding DataContext.Players/Login.IsRunningSession, ElementName=TabControlPlayers}" Value="False">  <-- Binding?
                <Setter Property="ContentTemplate" Value="{StaticResource LoginContentDataTemplate}" />
                <Setter Property="ItemTemplate" Value="{StaticResource TabControlNewItemDataTemplate}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Window.DataContext>
    <vm:CcMainViewModel x:Name="MainViewModel" />
</Window.DataContext>

<Grid>
    <TabControl Name="TabControlPlayers"
                ItemsSource="{Binding Players}"
                Style="{DynamicResource TabControlStyle}"/>
</Grid>

ViewModels:

public class CcMainViewModel : CcViewModelBase
{
    public ObservableCollection<CcPlayerViewModel> Players { get; set; }
}

 public class CcPlayerViewModel : CcViewModelBase
{
    public CcLoginViewModel Login { get; set; }
}

public class CcLoginViewModel : CcViewModelBase
{
     public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                OnPropertyChanged();
            }
        }
    }

    public string Password
    {
        get { return password; }
        set
        {
            if (password != value)
            {
                password = value;
                OnPropertyChanged();
            }
        }
    }

    public bool IsRunningSession
    {
        get { return isRunningSession; }
        set
        {
            if (isRunningSession != value)
            {
                isRunningSession = value;
                OnPropertyChanged();
            }
        }
    }
}
c# wpf datatemplate datatrigger
1个回答
0
投票

您可以使用Eldar所说的DataTemplateSelector。我为您添加了一些代码片段。

步骤1-通过继承如下所示的“ DataTemplateSelector”来创建一个类,

public class TabControlContentTemplateSelector : DataTemplateSelector
{
    public DataTemplate LoginContentDataTemplate { get; set; }

    public DataTemplate PlayerContentDataTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var obj = item as CcPlayerViewModel;
        if (obj != null)
        {
            if(obj.Login.IsRunningSession)
            {
                return PlayerContentDataTemplate;
            }
        }

        return LoginContentDataTemplate;
    }
}

步骤2-创建另一个名为“ TabControlItemTemplateSelector”的类,该类继承自“ DataTemplateSelector”,如下所示,

public class TabControlItemTemplateSelector : DataTemplateSelector
{
    public DataTemplate TabControlNewItemDataTemplate { get; set; }

    public DataTemplate PlayerTabItemTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var obj = item as CcPlayerViewModel;
        if (obj != null)
        {
            if(obj.Login.IsRunningSession)
            {
                return PlayerTabItemTemplate;
            }
        }

        return TabControlNewItemDataTemplate ;
    }
}

步骤3-在.xaml中创建模板选择器,如下所示,

<local:TabControlContentTemplateSelector x:Key="TabControlItemTemplateSelector"
  LoginContentDataTemplate="{StaticResource LoginContentDataTemplate}"
  PlayerContentDataTemplate="{StaticResource PlayerContentDataTemplate}" />

    <local:TabControlItemTemplateSelector x:Key="TabControlContentTemplateSelector"
      TabControlNewItemDataTemplate="{StaticResource TabControlNewItemDataTemplate}"
       PlayerTabItemTemplate="{StaticResource PlayerTabItemTemplate}" />

步骤4-同时使用上述TemplateSelector并将它们分配给您的TabControl,就像下面的.xaml中一样

<TabControl Name="TabControlPlayers" ItemsSource="{Binding Players}"
            ItemTemplateSelector="{StaticResource TabControlItemTemplateSelector}"
            ContentTemplateSelector="{StaticResource TabControlContentTemplateSelector}"/>

注意:-在这种情况下,除非对TabControl进行更多操作,否则将不使用现有的“ TabControlStyle”。

您现有的DataContext仍然可以正常工作。

尝试一下,如果您遇到任何其他问题,请告诉我们。

© www.soinside.com 2019 - 2024. All rights reserved.