【发布时间】:2014-01-10 10:24:39
【问题描述】:
我在 WPF .Net 中有一个 TabControl 项目,其中包含 2 个内容模板和 2 个 tabitems 模板。我将自己的对象绑定为 ItemsSource,它有两个属性:
public bool IsPrivMsgChannel
{
get
{
return _IsPrivMsgChannel;
}
set
{
_IsPrivMsgChannel = value;
if (_IsPrivMsgChannel)
{
Joined = true;
}
}
}
和
public bool Joined
{
get
{
return _Joined;
}
set
{
_Joined = value;
// Notify the UI thread!
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Joined"));
}
}
第一个属性将在对象初始化时设置,它不会改变。第二个属性可能会发生变化,然后它会向 UI 发送通知。
我想根据 IsPrivMsgChannel 属性更改我的 TabContol 的 TabItem 模板,并根据 Joined 属性更改 TabContent 模板。
更新我的解决方案现在看起来像这样:
<TabControl x:Name="Channels" Grid.Column="0" Grid.Row="1" TabStripPlacement="Left" SelectionChanged="ChannelChanged" ContentTemplateSelector="{StaticResource MyContentTemplateSelector}" ItemTemplateSelector="{StaticResource MyItemTemplateSelector}">
<TabControl.Resources>
<DataTemplate x:Key="TabItemChannelTemplate">
<Label HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding Name}"></Label>
</DataTemplate>
<DataTemplate x:Key="TabItemPrivChatTemplate">
<Label HorizontalAlignment="Center" VerticalAlignment="Center" Content="Private"></Label>
</DataTemplate>
<DataTemplate x:Key="TabContentDisconnected">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding Description}"></Label>
<Button Grid.Column="0" Grid.Row="1" Width="100" Content="Enter this channel" Click="Enter_Channel"></Button>
</Grid>
</DataTemplate>
<DataTemplate x:Key="TabContentConnected">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding Description}"></Label>
<Button Grid.Column="0" Grid.Row="1" Width="100" Content="Leave this channel" Click="Leave_Channel"></Button>
<DockPanel Grid.Column="0" Grid.Row="2">
<ListBox x:Name="GameList" HorizontalContentAlignment="Stretch" ItemTemplate="{DynamicResource GameListTemplate}" ItemsSource="{Binding GameList}" Visibility="{Binding CanHost, Converter={StaticResource BoolToVisibilityConverter}}" DockPanel.Dock="Top" Height="250" SelectionChanged="GameListSelectionChanged"></ListBox>
<ScrollViewer VerticalScrollBarVisibility="Auto" ScrollChanged="MessageScrollChanged" DockPanel.Dock="Bottom">
<TextBox x:Name="Messages" Text="{Binding Messages}" TextWrapping="Wrap"></TextBox>
</ScrollViewer>
</DockPanel>
</Grid>
</DataTemplate>
<Style TargetType="{x:Type TabItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Joined}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource TabContentConnected}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TabControl.Resources>
</TabControl>
所以我做了两个 DataTemplateSelector 类。一个是为项目选择模板,一个是为内容选择模板。
但由于 Joined 属性可以更改,因此我必须创建 Style.Triggers 来处理该属性是否更改并在需要时更改 ContentTemplate。
我唯一不明白的是为什么触发器只适用于 Style 的 TargetType TabItem 而不是 TargetType TabControl。
【问题讨论】:
标签: .net wpf templates tabcontrol tabitem