结合与X:绑定,使用静态资源作为默认及其在不同的DataContext [英] Binding vs. x:Bind, using StaticResource as a default and their differences in DataContext

查看:125
本文介绍了结合与X:绑定,使用静态资源作为默认及其在不同的DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了半天的更好试图用做的的ItemTemplate A 的ListView 用户控件配置通过的DependencyProperty 上的方式说用户控件。我遇到关于两个不同的绑定方法在Windows 10平台UAP(绑定和提供一些奇怪的不一致 X:绑定

I've spent the better of half a day trying to make the ItemTemplate of a ListView with a UserControl configurable through means of a DependencyProperty on said UserControl. I've come across some weird inconsistencies regarding the two different Binding methods available on Windows 10 UAP platform ( Binding and x:Bind).

用户控件看起来像这样,是一个自定义日历组件的一部分。

The UserControl looks like this and is part of a 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 的DependencyProperty 用户控件

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

这是改变根页面样式之一的ListView 在一个或其他方式,像这样。

Which is altered on one of the root pages to style the ListView in one way or the other, like so.

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

这实际上是一个工作版本,但我不得不鼓捣与它周围不少。第一次尝试是由我都取得了 X:绑定绑定并没有的DataContext RelativePanel 用户控件就是现在。 X:绑定根页面设置一个值 EventItemTemplate 时,将是功能,但它不能使用由指定的默认​​的DataTemplate 静态资源当根页面并没有指定任何。 绑定,另一方面将使用默认的的DataTemplate 在任何时候,甚至当根页有一组的其他值 EventItemTemplate

This is actually a working version, but I had to tinker around with it quite a bit. The first attempts were made by me with both x:Bind and Binding and without the DataContext on the RelativePanel as the UserControl is now. x:Bind would be functional when setting a value to EventItemTemplate in the root page, but it would not be able to use the default DataTemplate specified by the StaticResource when the root page did not specify anything. Binding on the other hand would use the default DataTemplate at all times, even when the the root page had a set an other value to EventItemTemplate.

通过设置的DataContext RelativePanel 用户控件 绑定开始工作就像想这一点。 X:绑定仍显示相同的行为。

By setting the DataContext on the RelativePanel to the UserControl Binding started worked like wanted it too. x:Bind still shows the same behavior.

现在我明白绑定默认不绑定到的 用户控件 DataContext的,但我仍然不能完全肯定,为什么 X:绑定不起作用。这是预期的行为,或有与我在这里的整个计划的东西错了,是我想出了只是一个幸运的黑客?

Now I understand that Binding does not by default bind to the UserControl's DataContext, but I'm still not entirely sure why x:Bind doesn't work. Is this intended behavior or is there something wrong with my entire scheme here and is what I came up with just a lucky hack?

推荐答案

在{X:}绑定标记扩展,新的Windows 10是{}绑定的替代。 {X:}绑定缺少一些{}绑定的功能,但它在更短的时间,比{}绑定更少的内存中运行,并且支持更好的调试

The {x:Bind} markup extension—new for Windows 10—is an alternative to {Binding}. {x:Bind} lacks some of the features of {Binding}, but it runs in less time and less memory than {Binding} and supports better debugging.

在XAML加载时间,{X:}绑定转换成你能想到的一个绑定对象,这个对象从数据源的属性获取的值。绑定对象可以任选地配置在数据源属性的值来观察变化和刷新本身基于这些变化。它也可以选择配置在其自身的价值变动推回源属性。由{X:}绑定创建绑定对象和{结合}在很大程度上是功能上等同。但{X:}绑定执行专用code,它产生在编译时,和{}绑定使用通用运行时对象的检查。因此,{X:绑定}绑定(通常称为对已编译的绑定)拥有强大的性能,使您在code设置断点提供您结合前pressions的编译时验证,并支持调试正在为您的网页局部类生成的文件。这些文件可以在你的OBJ文件夹中找到,像名(C#).g.cs。

At XAML load time, {x:Bind} is converted into what you can think of as a binding object, and this object gets a value from a property on a data source. The binding object can optionally be configured to observe changes in the value of the data source property and refresh itself based on those changes. It can also optionally be configured to push changes in its own value back to the source property. The binding objects created by {x:Bind} and {Binding} are largely functionally equivalent. But {x:Bind} executes special-purpose code, which it generates at compile-time, and {Binding} uses general-purpose runtime object inspection. Consequently, {x:Bind} bindings (often referred-to as compiled bindings) have great performance, provide compile-time validation of your binding expressions, and support debugging by enabling you to set breakpoints in the code files that are generated as the partial class for your page. These files can be found in your obj folder, with names like (for C#) .g.cs.

您可以在这里阅读更多的细节。的 {X:绑定}标记扩展

you can read more details here.{x:Bind} markup extension.

这篇关于结合与X:绑定,使用静态资源作为默认及其在不同的DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆