简体   繁体   中英

How do i correctly use ContentTemplateSelector with ContentControl?

I have lots of ContentControl objects and I need a border around a few of them, not all. I was able to get it working with the below code:

<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>

I want to move the entire ContentTemplate section to an other resource file and merge them. I added a new resource file and moved the below part of code to it.

<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>

When I try to change the actual ContentControl code to this, I get an error saying

"An object of type System.Windows.Controls.ControlTemplate cannot be applied to a property that expects the type 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>

How do I resolve this error?

An object of type ControlTemplate cannot be applied to a property that expects the type DataTemplateSelector

This error message is very explicit. You are assigning a ControlTemplate to a ContentControl.DataTemplateSelector . Type mismatch. You are assigning

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

to

<ContentControl ContentTemplateSelector="{StaticResource BalanceAmountControls}"> .

Solution:
You need to implement your own 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;
    }
}

Apply the template selector:

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

Now you need to create the proper DataTemplate resources which describe the individual visual appearance of the data that you have to bind to the ContentControl.Content property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM