I spent more than half a day trying to make the ItemTemplate ListView with UserControl custom using DependencyProperty on the specified UserControl . I came across some strange inconsistencies regarding the two different Binding methods available on the Windows 10 UAP platform ( Binding and x:Bind ).
UserControl looks like this and is part of the custom calendar component.
<UserControl x:Class="FlowDesigner.UserControls.CalendarDayView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:FlowDesigner.UserControls" xmlns:vw="using:FlowDesigner.ViewModels" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:uc="using:FlowDesigner.UserControls" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" x:Name="DateControl"> <UserControl.Resources> <DataTemplate x:Key="DefaultDataTemplate" x:DataType="vw:Event" > <uc:EventListTemplate IsToday="{Binding Date, Converter={StaticResource IsTodayConverter}}" Date="{Binding Date, Mode=OneWay}" Summary="{Binding Path=Summary, Mode=OneWay}" /> </DataTemplate> </UserControl.Resources> <RelativePanel Background="White" BorderBrush="Black" BorderThickness="1" DataContext="{Binding ElementName=DateControl}"> <TextBlock x:Name="DayText" TextAlignment="Center" VerticalAlignment="Center" /> <TextBlock x:Name="MonthText" TextAlignment="Center" VerticalAlignment="Center" RelativePanel.RightOf="DayText" /> <ListView x:Name="EventList" ItemsSource="{x:Bind Events, Mode=OneWay}" ItemTemplate="{Binding Path=EventItemTemplate, Mode=OneWay, FallbackValue={StaticResource DefaultDataTemplate}, TargetNullValue={StaticResource DefaultDataTemplate}}" RelativePanel.Below="DayText" RelativePanel.AlignLeftWithPanel="True" RelativePanel.AlignRightWithPanel="True"> </ListView> </RelativePanel> </UserControl>
EventItemTemplate is a DependencyProperty UserControl .
public DataTemplate EventItemTemplate { get { return (DataTemplate)GetValue(EventItemTemplateProperty); } set { SetValue(EventItemTemplateProperty, value); } } public static readonly DependencyProperty EventItemTemplateProperty = DependencyProperty.Register("EventItemTemplate", typeof(DataTemplate), typeof(CalendarDayView), new PropertyMetadata(null));
What has changed on one of the root pages so that the ListView style is anyway, for example.
<Style TargetType="uc:CalendarDayView"> <Setter Property="EventItemTemplate"> <Setter.Value> <DataTemplate x:DataType="vw:Event" > <TextBlock Text="{Binding Summary, Mode=OneWay}" /> </DataTemplate> </Setter.Value> </Setter> </Style>
This is actually a working version, but I had to work a little with it. The first attempts were made by me with x:Bind and Binding and without a DataContext on a RelativePanel , because now UserControl . x:Bind will function when setting the value to EventItemTemplate on the root page, but it will not be able to use the default DataTemplate specified by StaticResource when the root page did not specify anything. Binding , on the other hand, will always use the default DataTemplate , even if the root page had a different EventItemTemplate value.
By setting the DataContext in the RelativePanel in the UserControl Binding , it started working the way it wanted. x:Bind still shows the same behavior.
Now I understand that Binding does not bind to the UserControl DataContext by default, but I'm still not quite sure why x:Bind not working. Is this the intended behavior, or is there something wrong with my entire circuit here, and is it that I came up with just a good hack?