WPF DataTemplate 属性设置在 Content

标签 wpf datatemplate binding templatebinding

WPF 的新手并有选项卡,并且在每个选项卡中,内容都显示在弯曲的角落面板/窗口/任何你想要的东西中。我不确定如何执行此操作( 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>

正如您在 background 属性中看到的那样,我不想在内容中设置背景颜色,但不知道如何设置。我在这里使用它。
<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 是错误的还是有其他方法?

我可能可以直接在内容上设置背景,并从 mthe 模板中的填充更改为内容中的边距,但在一些类似的情况下不起作用,只需要设置一次会更好。

编辑:

根据建议,我更改为 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>

最佳答案

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

关于WPF DataTemplate 属性设置在 Content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3118266/

相关文章:

.net - WPF:如何使用嵌套(分层)控件实现拖放

.net - 如何摆脱烦人的 Horizo​​ntalContentAlignment 绑定(bind)警告?

jsf - 将 bean 属性绑定(bind)到 JSF 中的元素时出现问题

wcf - 为 HTTPS 和 HTTP 托管相同服务时为 "A registration already exists for URI"

wpf - WPF 中的环境遮挡着色器效果?

c# - 绑定(bind)到绑定(bind)元素?

c# - WPF - 如何将文本框添加到包含 ItemsControl 的项目的面板

listview - 更改 ListView 的 ItemPanelTemplate 方向

c# - 从一个 DataTemplate 切换到另一个

c# - BindingError 会降低应用程序性能吗?