Binding versus x: Binding, using StaticResource by default and their differences in the DataContext

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?

+5
source share
1 answer

From Markup Extension {x: Bind} :

The {x: Bind} markup extension, new to Windows 10, is an alternative to {Binding}. {x: Bind} lacks some {Binding} functions, but it works less time and less memory than {Binding} and supports better debugging.

At boot time, XAML {x: Bind} is converted to what you can think of as a binding object, and this object gets the value from the property in the data source. The binding object can be further configured to observe the change in the value of the data source property and update based on these changes. It can also be configured to make changes to its value back to the original property. The binding objects created by {x: Bind} and {Binding} are basically functionally equivalent. But {x: Bind} runs the special code that it generates at compile time, while {Binding} uses general purpose object checking. Therefore, {x: Bind} bindings (often referred to as compiled bindings) are powerful, provide verification of your binding expressions at compile time, and support debugging, allowing you to set breakpoints in code files that are generated as partial classes for your page. These files can be found in your obj folder with names like (for C #). G.cs.

+8
source

All Articles