locked
How to use templatebinding from DataTemplate

    Question

  • I have the following code:

    <Style TargetType="local:LabelledList">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:LabelledList">
                        <ListView DataContext="{TemplateBinding ListSource}" ItemsSource="{Binding}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="auto" />
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="auto" />
                                        </Grid.RowDefinitions>
    
                                        <ContentControl Grid.Row="0" Content="{Binding}" ContentTemplate="{TemplateBinding ItemHeaderTemplate}" />
                                        <!-- ... -->
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    TemplateBinding inside DataTemplate isn't working.

    How do I make it work?


    Wednesday, January 07, 2015 4:47 PM

Answers

  • I made a workaround for you:

    <Style TargetType="local:LabelledList">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:LabelledList">
                        <ContentControl x:Name="hiddenItems" Visibility="Collapsed"
                                ContentTemplate="{TemplateBinding ItemHeaderTemplate}" />
                        <ListView DataContext="{TemplateBinding ListSource}" ItemsSource="{Binding}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="auto" />
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="auto" />
                                        </Grid.RowDefinitions>
    
                                        <ContentControl Grid.Row="0" Content="{Binding}" ContentTemplate="{Binding ContentTemplate, ElementName=hiddenItems}" />
                                        <!-- ... -->
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    Does it work?

    I wonder if there is any simpler way?




    Thursday, January 08, 2015 10:41 PM

All replies

  • TemplateBinding is pointing to the control. so in your control LabelledList you have a dependency property ListSource? can you share a sample of what you have?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Wednesday, January 07, 2015 6:31 PM
  • I have DependencyProperty in my LabelledList class:

        public sealed class LabelledList : Control
        {
            public LabelledList()
            {
                this.DefaultStyleKey = typeof(LabelledList);
            }
    		
            // ...
    		
            public DataTemplate ItemHeaderTemplate
            {
                get { return (DataTemplate)GetValue(ItemHeaderTemplateProperty); }
                set { SetValue(ItemHeaderTemplateProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for ItemHeaderTemplate.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ItemHeaderTemplateProperty =
                DependencyProperty.Register("ItemHeaderTemplate", typeof(DataTemplate), typeof(LabelledList), new PropertyMetadata(null));
    
            // ...
        }

    other templatebindings work fine.

    During design i'm getting error: ItemHeaderTemplate is not recognized or is not accessible, and during execution it doesn't work.




    • Edited by smokeing Wednesday, January 07, 2015 7:41 PM
    Wednesday, January 07, 2015 7:35 PM
  • I made a workaround for you:

    <Style TargetType="local:LabelledList">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:LabelledList">
                        <ContentControl x:Name="hiddenItems" Visibility="Collapsed"
                                ContentTemplate="{TemplateBinding ItemHeaderTemplate}" />
                        <ListView DataContext="{TemplateBinding ListSource}" ItemsSource="{Binding}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="auto" />
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="auto" />
                                        </Grid.RowDefinitions>
    
                                        <ContentControl Grid.Row="0" Content="{Binding}" ContentTemplate="{Binding ContentTemplate, ElementName=hiddenItems}" />
                                        <!-- ... -->
                                    </Grid>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    Does it work?

    I wonder if there is any simpler way?




    Thursday, January 08, 2015 10:41 PM