如何正确使用ContentTemplateSelector和ContentControl?

时间:2019-07-18 09:01:54

标签: c# wpf xaml

我有很多ContentControl对象,但我需要在其中一些(不是全部)周围加上边框。我可以通过以下代码使用它:

<ContentControl Margin="2" Grid.Row="0" Grid.Column="1" BorderThickness="5" BorderBrush="Beige">
    <!-- ContentControl.Template to get the border -->
    <ContentControl.Template>
        <ControlTemplate TargetType="ContentControl">
            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
                <ContentPresenter
                        Content="{TemplateBinding Content}"
                        ContentTemplate="{TemplateBinding ContentTemplate}"
                        Cursor="{TemplateBinding Cursor}"
                        Margin="{TemplateBinding Padding}" />
            </Border>
        </ControlTemplate>
    </ContentControl.Template>
    <StackPanel>
        <Label Content="Paid" HorizontalAlignment="Right" />
        <Label Content="{Binding ReceivedAmount}" HorizontalAlignment="Right" />
    </StackPanel>
</ContentControl>

我想将整个ContentTemplate部分移到另一个资源文件中并合并它们。我添加了一个新的资源文件,并将下面的代码部分移到了该文件。

<ControlTemplate TargetType="ContentControl" x:Key="BalanceAmountControls">
    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
        <ContentPresenter
                Content="{TemplateBinding Content}"
                ContentTemplate="{TemplateBinding ContentTemplate}"
                Cursor="{TemplateBinding Cursor}"
                Margin="{TemplateBinding Padding}" />
    </Border>
</ControlTemplate>

当我尝试将实际的ContentControl代码更改为此时,我收到一条错误消息

  

“类型为System.Windows.Controls.ControlTemplate的对象不能为   应用于期望类型的属性   System.Windows.Controls.DataTemplateSelector“

<!-- Balance amount -->
<ContentControl Margin="2" Grid.Row="0" Grid.Column="2" BorderThickness="5" BorderBrush="Beige" ContentTemplateSelector="{StaticResource BalanceAmountControls}">
    <!-- ContentControl.Template to get the border -->
    <StackPanel>
        <Label Content="Balance" HorizontalAlignment="Right" />
        <Label Content="{Binding BalanceAmount}" HorizontalAlignment="Right" />
    </StackPanel>
</ContentControl>

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

  

类型为 ControlTemplate 的对象不能为   应用于期望类型的属性    DataTemplateSelector

此错误消息非常明确。您正在将ControlTemplate分配给ContentControl.DataTemplateSelector。类型不匹配。您正在分配

<ControlTemplate TargetType="ContentControl" x:Key="BalanceAmountControls">

<ContentControl ContentTemplateSelector="{StaticResource BalanceAmountControls}">

解决方案:
您需要实现自己的DataTemplateSelector

public class ContentControlDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate
        SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element != null && item != null && item is Task)
        {
            if (// your condition)
                return
                    element.FindResource("BalanceAmountControls") as  DataTemplate;
            else
                return
                    element.FindResource("NonBalanceAmountControls") as DataTemplate;
        }

        return null;
    }
}

应用模板选择器:

<ContentControl ContentTemplateSelector="{StaticResource ContentControlDataTemplateSelector}" />

现在,您需要创建适当的DataTemplate资源,这些资源描述必须绑定到ContentControl.Content属性的数据的单个视觉外观。