3

所以我TabControl在运行时将我的视图直接添加到 的 Items 集合中(而不是在它们周围创建 TabItems 并将这些 TabItems 添加到 TabControl)。视图公开了一个属性(围绕同名的 ViewModel 属性的包装器),该属性名为HasChanges我要绑定到TabItem的 Header 以显示星号 (*) 符号以识别具有未保存更改的选项卡,就像 VS 所做的那样。我已经尝试过使用 DataTemplates,但在访问DataTemplate. 这样做的正确方法是什么?这是我的几次尝试之一:

<TabControl.ItemTemplate>
    <DataTemplate DataType="UserControl">
        <StackPanel Orientation="Horizontal" Margin="0" Height="22">
            <TextBlock VerticalAlignment="Center" Text="{Binding HeaderText, RelativeSource={RelativeSource AncestorType=UserControl}}" />
            <TextBlock Text="*" Visibility="{Binding HasChanges, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource B2VConverter}}" />
        </StackPanel>
    </DataTemplate>
</TabControl.ItemTemplate>

请注意,我正在为这两个TextBlocks 尝试两种不同的绑定方法,但都不起作用。我的视图继承UserControl并公开属性HasChangesHeaderText.

4

1 回答 1

6

好的。我自己解决了。对于其他尝试实现类似 VS 的关闭按钮和未保存的更改星号的人,这里是模板:

<TabControl.ItemContainerStyle>
    <Style TargetType="{x:Type TabItem}">
        <Setter Property="HeaderTemplate" >
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="0" Height="22">
                        <TextBlock VerticalAlignment="Center" Text="{Binding RelativeSource={RelativeSource AncestorType=TabItem}, Path=Content.HeaderText}" />
                        <TextBlock Text=" *" ToolTip="Has unsaved changes" Visibility="{Binding Content.DataContext.HasChanges, RelativeSource={RelativeSource AncestorType=TabItem}, Converter={StaticResource B2VConverter}}" />
                        <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" Width="18" Height="18" 
                                Margin="6,0,0,0" Padding="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" 
                                Command="{Binding DataContext.TabClosingCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                                CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"
                                VerticalAlignment="Center" Focusable="False">
                            <Grid Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                <Line StrokeThickness="3" StrokeStartLineCap="Round"  StrokeEndLineCap="Round" Stroke="Gray" X1="1" Y1="1" X2="9" Y2="9" HorizontalAlignment="Center" VerticalAlignment="Center" />
                                <Line StrokeThickness="3" StrokeStartLineCap="Round" StrokeEndLineCap="Round" Stroke="Gray" X1="1" Y1="9" X2="9" Y2="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Grid>
                        </Button>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</TabControl.ItemContainerStyle>

结果是一个优雅的基于绘图的按钮,外观扁平。您的视图必须实现布尔值HasChangesHeaderText属性,另外您需要BooleanToVisibilityConverter在资源部分中定义一个名为 B2VConverter。

于 2013-03-19T06:33:52.603 回答