WPF:TabItem标头中的绑定

时间:2013-11-25 15:52:56

标签: c# wpf tabitem

当TabItem的内容被修改(绑定数据)时,我想在标签项标题“*”(星号)中显示。 我有以下TabItem样式:

<TabControl.Resources>
    <Style TargetType="{x:Type TabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <Border Name="_border" 
                                Margin="0,0,0,0" 
                                Padding="0 0 5 0" 
                                Background="Transparent" 
                                BorderBrush="Black" 
                                BorderThickness="0,0,0,0" 
                                CornerRadius="1">
                            <StackPanel Orientation="Horizontal" Margin="10 2 0 2">
                                <TextBlock Foreground="Black" Name="_header">
                                    <ContentPresenter VerticalAlignment="Center" 
                                                      HorizontalAlignment="Center"
                                                      ContentSource="Header" 
                                                      RecognizesAccessKey="True">
                                    </ContentPresenter>
                                </TextBlock>
                                <Button Name="_close"
                                        Visibility="Hidden"
                                        Style="{StaticResource _closeButtonStyle}" 
                                        CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}"
                                        Click="CloseTab_Click"
                                        BorderThickness="0"
                                        Margin="10 0 0 0"
                                        Width="16"
                                        Height="16">
                                    <Image Source="Images/delete_icon16_white.png"
                                           Width="10"
                                           Height="10"
                                           Cursor="Hand"/>
                                </Button>
                            </StackPanel>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="_header" Property="Foreground" Value="White"/>
                            <Setter TargetName="_close" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="_border" Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}"/>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True"/>
                                <Condition Property="IsSelected" Value="False"/>
                            </MultiTrigger.Conditions>
                            <Setter TargetName="_header" Property="Foreground" Value="White"/>
                            <Setter TargetName="_close" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="_border" Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.MenuHighlightColorKey}}"/>
                                </Setter.Value>
                            </Setter>
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</TabControl.Resources>

当绑定数据的属性 IsModified 设置为true时,如何添加此提及“*”的样式?

感谢您的回复

1 个答案:

答案 0 :(得分:1)

使用Text =“*”在标题上创建一个TextBlock。

然后将其可见性与IsModified属性和booleanToVisibility Converter

绑定

编辑: 绑定可见性

Visibility="{Binding IsModified, Converter={StaticResource booleanToVisibilityConverter}}"

在您的资源文件中

<BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>

EDIT2:不知道您是否需要此信息,只是针对此案例。 HeaderTemplate的方法

<Setter Property="HeaderTemplate">
    <Setter.Value>
      <DataTemplate>
        <StackPanel>
          <TextBlock Text="{Binding Content}" />
          <TextBlock Text="*" Visibility="{Binding IsModified, Converter={StaticResource booleanToVisibilityConverter}}"/>
        </StackPanel>
      </DataTemplate>
    </Setter.Value>