Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 无法设置选项卡控件的样式_Wpf_Mvvm - Fatal编程技术网

Wpf 无法设置选项卡控件的样式

Wpf 无法设置选项卡控件的样式,wpf,mvvm,Wpf,Mvvm,我慢慢习惯了样式设计,尤其是关于模板(DataTemplates、Templates、ControlTemplates等) 然而,我迷路了 我试图让我的标签项全部为白色(我还没有到触发的程度,所以所选的项目不会显示),并且内容为灰色。整个事情是无边界的 我正在遵循,我的精简版本是 <TabControl ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab, UpdateSourceTrigger=PropertyC

我慢慢习惯了样式设计,尤其是关于模板(DataTemplates、Templates、ControlTemplates等)

然而,我迷路了

我试图让我的标签项全部为白色(我还没有到触发的程度,所以所选的项目不会显示),并且内容为灰色。整个事情是无边界的

我正在遵循,我的精简版本是

<TabControl ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab, UpdateSourceTrigger=PropertyChanged}" Margin="20">
            <TabControl.Resources>
                <Style TargetType="{x:Type TabControl}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TabControl}">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <Border Background="Red">
                                        <ContentPresenter x:Name="PART_SelectedContentHost"
                          Margin="4"
                          ContentSource="SelectedContent" />
                                    </Border>

                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <Style TargetType="{x:Type TabItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TabItem}">
                                <Grid>

                                        <ContentPresenter x:Name="ContentSite"
                          VerticalAlignment="Center"
                          HorizontalAlignment="Center"
                          ContentSource="Header"
                          Margin="12,2,12,2"
                          RecognizesAccessKey="True" />
                                </Grid>

                            </ControlTemplate>

                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.Resources>
        </TabControl>
这没什么区别

我看不出我应该做什么,也看不出与MSDN文章相比我错在哪里

我的最终目标是这样(未选择的TabItems是白色的(我知道我可以使用触发器),TabPanel是白色的,内容是灰色的)。模糊的部分是标题(文本):


我的问题是,如何显示Title属性并从默认TabControl的TabItem中删除边框

如果要重新定义
TabItem
的外观,您应该只为
TabItem.Template
属性提供一个值。。。我猜您只需要一个普通的
TabItem
。与其尝试定义新的
ControlTemplate
来绑定
Title
属性,不如将其设置为
TabItem.Header
值:

<Style TargetType="{x:Type TabItem}">
    <Setter Property="Header" Value="{Binding Title}" />
    ...
</Style>

...


我是这样开始的,但是,当我这样做时,边界仍然存在,所以我认为我必须覆盖它。虽然您的建议确实有效(它显示了标题),但我仍然可以删除边框吗?如果您想重新定义
选项卡项
的外观,可以使用
模板
属性。。。我只是错误地认为你那样做是偶然的。
<Style TargetType="{x:Type TabItem}">
    <Setter Property="Header" Value="{Binding Title}" />
    ...
</Style>
<TabControl ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab, UpdateSourceTrigger=PropertyChanged}" Margin="20">
            <TabControl.Resources>
                <Style TargetType="{x:Type TabControl}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TabControl}">

                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                        <RowDefinition Height="*"></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <Border Background="White">
                                        <TabPanel Name="HeaderPanel"
                                            Grid.Row="0"
                                            Panel.ZIndex="1" 
                                            Margin="0" 
                                            IsItemsHost="True"
                                            KeyboardNavigation.TabIndex="1"
                                            HorizontalAlignment="Left"/>
                                    </Border>
                                    <Border Background="Teal" Grid.Row="1">
                                        <ContentPresenter ContentSource="SelectedContent"></ContentPresenter>
                                    </Border>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Background" Value="#FFF3F3F3"></Setter>
                    <Setter Property="BorderThickness" Value="0" />
                </Style>
                <Style TargetType="{x:Type TabItem}" >
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TabItem}">
                                <Grid>
                                        <ContentPresenter Content="{Binding Title}" Margin="20,0,20,0"  />
                                </Grid>

                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="Background" Value="Orange" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>

                        </Setter.Value>
                    </Setter>
                    <!--<Setter Property="Background" Value="White"></Setter>
                    <Setter Property="Header" Value="{Binding Title}" />
                    <Setter Property="BorderThickness" Value="0" />


                </Style>
                 </TabControl.Resources>
        </TabControl>