【问题标题】:WPF DataTemplate property set at ContentWPF DataTemplate 属性设置在 Content
【发布时间】:2011-03-08 06:59:24
【问题描述】:

WPF 新手并具有选项卡,并且在每个选项卡中,内容都显示在弯曲的角落面板/窗口/whateveryouwannacallit 中。我不确定如何执行此操作(Style、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 并将其放入样式中。这解决了背景问题,但创建了一个更大的问题。现在内容不会出现。我在博客here 上读到,放置一个 targetType 可以解决这个问题,但它并没有解决我的问题。代码现在看起来像这样,并且还更改了 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>

【问题讨论】:

    标签: wpf datatemplate binding templatebinding


    【解决方案1】:

    可能是因为 TemplateBinding 不适用于 DataTemplate。 Check this question for details

    即使它有效,您只需要ControlTemplate 而不是数据模板。

    【讨论】:

    • 谢谢,我确实知道 TemplateBinding 不起作用我把它放在那里以显示我想要的东西。我真的不知道 DataTemplate 和 ControlTemplate 之间的区别,所以我永远不知道该使用哪个。
    • 第一个链接坏了。
    【解决方案2】:

    使用 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}"/>
    

    【讨论】:

    • 这适用于背景,但现在我的内容没有出现在边框内。
    • 终于想通了,不得不将 ContentPresenter Content 属性更改为 {TemplateBinding Content} 。
    猜你喜欢
    • 2010-11-18
    • 1970-01-01
    • 2010-10-16
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多