编程语言
首页 > 编程语言> > c# – WPF:TabItem标头中的绑定

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时,如何添加到提及“*”的这种样式?

谢谢你的回复

解决方法:

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

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

编辑:
绑定可见性

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

在您的资源文件中

<BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>

编辑2:不知道你是否需要这个信息,只是为了这个案子. HeaderTemplate的一种方法

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

标签:c,wpf,tabitem
来源: https://codeday.me/bug/20190708/1407196.html