WPF DataTemplate属性设置为“内容" [英] WPF DataTemplate property set at Content

查看:509
本文介绍了WPF DataTemplate属性设置为“内容"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WPF的新增功能,具有选项卡,并且在每个选项卡中,内容均以弯曲的角面板/窗口/您要调用的内容显示.我不确定如何执行此操作(Style,ControlTemplate),但决定采用DataTemplate方式.

New to WPF and have Tabs and in each tab the content is presented in a curved corner panel/window/whateveryouwannacallit. I wasn't sure how to do this ( Style, ControlTemplate ) but decided to go the DataTemplate way.

所以现在我有了这个DataTemplate:

So now I have this 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>

正如您所看到的background属性,我不想在内容上设置背景颜色,但不知道如何设置.在这里我用它.

As you can see with the the background property I wan't to set the background color at the content but Don't know how. Here I use it.

<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是错误的还是还有其他方法?

Is using DataTemplate wrong here or is there any other way?

我可以直接在内容上设置背景,并从模板中的填充更改为内容中的空白,但是在某些类似的情况下,这是行不通的,最好只设置一次即可.

I could probably set the background straight on the content and change from padding in mthe template to margin in the content but in some similiar situations that wouldn't work and it's nicer to only have to set it once.

根据建议,我更改为ControlTemplate并将其放入样式中.这解决了背景问题,但产生了更大的问题.现在内容将不会出现.我在此处上读到的博客,将targetType可以解决这个,但是没有解决我的问题.该代码现在看起来像这样,并且还更改了ContentControl以使用样式而不是Template.

As per advice I changed to ControlTemplate and also put it inside a style. This solves the Background problem but creates a bigger one. Now the content won't appear. I read on a blog here that putting a targetType solves this but it didn't solve my problem. The code looks like this now and also changed the ContentControl to use the style instead of Template.

<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

Use ControlTemplate instead 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

Use Template instead of ContentTemplate

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

这篇关于WPF DataTemplate属性设置为“内容"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆