WPF的新手并具有标签,在每个选项卡中,内容都在弯曲的角面板/窗口/whateveryouwannacallit中呈现。我不确定该如何执行此操作(样式,ControlTemplate),但决定使用DatateMplate方式。

因此,现在我有了这个datatemplate:

<DataTemplate x:Key="TabContentPresenter" >
    <Border Margin="10"
            BorderBrush="{StaticResource DarkColorBrush}"
            CornerRadius="8"
            BorderThickness="2"
            Grid.Row="0"
            Padding="5" 
            Background="{TemplateBinding Background}">         

        <ContentPresenter Content="{Binding}" />

    </Border>
</DataTemplate>

正如您可以看到的背景属性,我不会在内容上设置背景颜色,但不知道如何设置背景颜色。我在这里使用它。

<Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="120"/>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Background="White">


                <!-- Something Here -->

            </ContentControl>

            <ContentControl ContentTemplate="{StaticResource TabContentPresenter}" Grid.Row="1" Background="Blue">

                <!-- Something Here -->

            </ContentControl>

        </Grid>

在这里使用DataTemplate吗?还是有其他方法吗?

我可能可以将背景直接设置在内容上,并从模板中的填充到内容的边距,但是在某些相似的情况下,这些情况不起作用,只需要设置一次就可以更好。

编辑:

根据建议,我更改为ControlTemplate,并将其放入样式中。这解决了背景问题,但会产生更大的问题。现在内容不会出现。我在博客上阅读 这里 使目标型的解决方案可以解决这一问题,但并不能解决我的问题。该代码现在看起来像这样,还更改了ContentControl以使用样式而不是模板。

<Style x:Key="TabContentPresenter" TargetType="ContentControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Border Margin="10"
            BorderBrush="{StaticResource DarkColorBrush}"
            CornerRadius="8"
            BorderThickness="2"
            Grid.Row="0"
            Background="{TemplateBinding Background}">

                    <ContentPresenter Content="{Binding}" />

                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
有帮助吗?

解决方案

使用ControlTemplate代替DataTemplate

 <ControlTemplate  x:Key="TabContentPresenter">
        <Border Margin="10" 
                    CornerRadius="8" 
                    BorderThickness="2" 
                    Grid.Row="0" 
                    Padding="5"  
                    Background="{TemplateBinding Background}">
            <ContentPresenter Content="{Binding}"/>
        </Border>
    </ControlTemplate>

使用模板而不是ContentTemplate

<ContentControl  Background="Green" Template="{StaticResource  TabContentPresenter}"/>

其他提示

可能是因为 TemplateBinding 不使用DataTemplate。 检查此问题以获取详细信息.

即使它起作用,您所需要的只是 ControlTemplate 而不是DataTemplate。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top