【问题标题】:XAML- How to add images to a <TabControl> tagXAML-如何将图像添加到 <TabControl> 标记
【发布时间】:2016-05-13 13:42:10
【问题描述】:

我正在为我的应用程序开发一个“选项卡式”GUI,使用 XAML 将 GUI 呈现给用户。

我目前正在显示几个选项卡,每个选项卡都显示应用程序的不同方面。

我有几个图标显示应用程序与远程服务器的“连接状态”(即,如果应用程序连接到服务器,则显示一个图像,如果未连接,则显示另一个图像)。这些图像当前显示在一个选项卡式显示器中,但我想将它们移动到“选项卡”栏(在应用程序窗口的最右侧),以便它们始终可见,无论选项卡是什么用户当前正在查看。

我的 XAML 目前的结构如下:

<Grid>
    <TabControl ...>
        <TabItem>
            ...
        </TabItem>
        <TabItem ...>
            <StackPanel>
                <Grid>
                    ...
                    <Image ... />
                    <Image ... />
                </Grid>
            </StackPanel>
        </TabItem>
    </TabControl>
</Grid>

基本上,显示&lt;Image&gt;标签中的一张图片表示应用程序已连接到服务器,显示另一张图片表示应用程序未连接到服务器。它们都被放置在应用程序内部的相同位置,并且有一个方法可以检查应用程序是否连接到服务器,并根据该方法返回的值显示适当的图像。

我想要做的是将这些&lt;Image&gt; 标签移动到主&lt;TabControl&gt; 标签中,以便这些图像可以显示在“标签菜单”上,但在窗口的最右侧(如用户可用的各种选项卡显示在最左侧)。这意味着无论用户当前正在查看哪个标签,都会显示这些图像。

有没有办法可以做到这一点?我已经尝试在&lt;TabControl&gt; 标签内直接添加&lt;Image&gt; 标签,但是当我运行我的应用程序时,图像不会显示......有人对我如何实现我想要在这里做的事情有任何建议吗?

【问题讨论】:

    标签: c# wpf image xaml


    【解决方案1】:

    要添加虚构的TabBar,您应该在TabControl 的Template 中创建新的RowDefinition。让我举个例子:

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="WpfApplication2.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="TabItem.Selected.Background" Color="#FFFFFF"/>
        <SolidColorBrush x:Key="TabItem.Selected.Border" Color="#ACACAC"/>
        <Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}">
            <Setter Property="Padding" Value="2"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Background" Value="{StaticResource TabItem.Selected.Background}"/>
            <Setter Property="BorderBrush" Value="{StaticResource TabItem.Selected.Border}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid x:Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition x:Name="ColumnDefinition0"/>
                                <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                                <RowDefinition x:Name="RowDefinition2" Height="0.1*"/>
                                <RowDefinition x:Name="RowDefinition1" Height="*"/>
                            </Grid.RowDefinitions>
                            <TabPanel x:Name="headerPanel" Background="Yellow" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                            <StackPanel Grid.Row="1" Orientation="Horizontal" FlowDirection="RightToLeft">
                                <Image Source="/Images/1.png" />
                                <Image Source="/Images/2.png" />
                                <Image Source="/Images/3.png" />                                
                            </StackPanel>
                            <Border x:Name="contentPanel" BorderBrush="Green" BorderThickness="5" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="2" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                                <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="TabStripPlacement" Value="Bottom">
                                <Setter Property="Grid.Row" TargetName="headerPanel" Value="1"/>
                                <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/>
                                <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/>
                                <Setter Property="Margin" TargetName="headerPanel" Value="2,0,2,2"/>
                            </Trigger>
                            <Trigger Property="TabStripPlacement" Value="Left">
                                <Setter Property="Grid.Row" TargetName="headerPanel" Value="0"/>
                                <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/>
                                <Setter Property="Grid.Column" TargetName="headerPanel" Value="0"/>
                                <Setter Property="Grid.Column" TargetName="contentPanel" Value="1"/>
                                <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                                <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                                <Setter Property="Margin" TargetName="headerPanel" Value="2,2,0,2"/>
                            </Trigger>
                            <Trigger Property="TabStripPlacement" Value="Right">
                                <Setter Property="Grid.Row" TargetName="headerPanel" Value="0"/>
                                <Setter Property="Grid.Row" TargetName="contentPanel" Value="0"/>
                                <Setter Property="Grid.Column" TargetName="headerPanel" Value="1"/>
                                <Setter Property="Grid.Column" TargetName="contentPanel" Value="0"/>
                                <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/>
                                <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/>
                                <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                                <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>
                                <Setter Property="Margin" TargetName="headerPanel" Value="0,2,2,2"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="FooTabControl" TargetType="{x:Type TabControl}">
            <!--This style is intended to be empty just for show that you can declare as many styles as you want-->
        </Style>
        <Style x:Key="FooButton"  TargetType="{x:Type Button}">
            <!--This style is intended to be empty just for show that you can declare as many styles as you want-->
        </Style>
    </Window.Resources>
    <Grid Name="grid">
        <TabControl Style="{DynamicResource TabControlStyle1}">
            <TabItem Header="1">1</TabItem>
            <TabItem Header="2">2</TabItem>
            <TabItem Header="3">3</TabItem>
        </TabControl>
    </Grid>
    </Window>
    

    【讨论】:

    • 您好,谢谢您的回答。问题是,我不想仅在单个 &lt;TabItem&gt; Header 中显示图像,而是希望在顶层 &lt;TabControl&gt; 中显示它们,以便它们在“标签栏”上可见' 无论用户当前正在查看哪个选项卡。例如,如果我在应用程序窗口中打开了三个选项卡,这些选项卡将显示在“选项卡栏”的左侧,用户可以通过单击选择查看哪个选项卡,然后页面内容将显示然后根据选择的选项卡进行更新。
    • 我想要做的是将这些图像添加到“标签栏”的右侧,以便它们始终可见,无论用户当前选择了哪个标签,正如他们所指出的那样整个应用程序的连接性,并且不仅仅与应用程序的某个特定方面相关。
    • 嘿,感谢您的更新-看起来这就是我想要做的。但是,我的 XML 中已经有一个 &lt;ControlTemplate TargetType="{x:Type Button}"&gt; 标记,所以当尝试添加 &lt;ControlTemplate TargetType="{x:Type TabControl}"&gt; 时,我收到一个编译错误,上面写着:The property 'Value' is set more than once.,或者如果我添加了您编写的所有代码,则会显示我想要的图像之一,但随后停止显示我拥有的其余内容,并且在 VS 的设计视图中出现错误,显示“元素“{Button}”由于出现问题而无法显示与
    • System.Windows.Controls.Button: 'TabControl' ControlTemplate TargetType 与模板化类型 'Button' 不匹配。
    • 我可以添加两个ControlTemplates 吗?如果是这样,我需要给他们不同的名字吗?或者,如果没有,我可以将您在帖子代码中提到的所有控件添加到我已经在使用的同一 ControlTemplate 中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2012-10-02
    相关资源
    最近更新 更多