小编Mar*_*kos的帖子

WPF 在控件上同时设置 DataTemplate 和 ControlTemplate 不起作用

我显然在这里遗漏了一些非常基本的东西。我发现了类似的问题,但从没有一个答案中我无法理解我做错了什么。

当我设置 ControlTemplate 时,我的 DataTemplate 未被选取。

我为我的问题创建了一个非常简单的示例:

<Window x:Class="WpfTesterProject.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTesterProject"
        Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Person}">
            <StackPanel>
                <TextBlock Text="{Binding FirstName}" />
                <TextBlock Text="{Binding LastName}" />
            </StackPanel>
        </DataTemplate>
    </Window.Resources>

    <ContentControl Content="{Binding Content}">
        <ContentControl.Template>
            <ControlTemplate>
                <Border BorderBrush="Blue" BorderThickness="2">
                    <ContentPresenter />
                </Border>
            </ControlTemplate>
        </ContentControl.Template>
    </ContentControl>
</Window>
Run Code Online (Sandbox Code Playgroud)

我想要做的是选择在运行时加载的用户定义的数据模板,但我也想,例如,将每个元素包装在边框中,无论用户模板是什么,或者即使他根本没有指定任何模板。

根据我从类似问题中读到的内容,我必须<ContentPresenter />在 ControlTemplate 中使用,但结果与删除它相同 - 仅显示边框。

wpf xaml

4
推荐指数
1
解决办法
1548
查看次数

标签 统计

wpf ×1

xaml ×1