【问题标题】:Dynamic binding in c# where we cannot use Compiled data binding(x:bind)c#中的动态绑定,我们不能使用编译数据绑定(x:bind)
【发布时间】:2020-05-07 11:54:07
【问题描述】:

我正在使用 c# 构建一个 UWP 应用程序,在其中我发现了 Compiled data binding(x:bind)。 它具有许多优点,例如性能、xaml 页面中的编译时错误、绑定事件等。 当我阅读有关已编译数据绑定的教程之一时,它清楚地指出我们不能在动态绑定对象的场景中使用 x:bind。我正在寻找一个示例,特别是对于使用 x:bind 无法解决的运行时绑定。 非常感谢具有动态对象绑定的一小段代码。 提前致谢

【问题讨论】:

    标签: c# uwp xbind


    【解决方案1】:

    当我不确定某个特定属性是否存在时,我通常会使用绑定。 示例:

    <DataTemplate x:DataType="Message">
        <StackPanel>
            <TextBlock Text="{x:Bind Title}" />
            // if binding fails, the value is set to 0
            <ProgressBar Value="{Binding Progress, FallbackValue=0}"
                         Maximum="1"
                         Background="Transparent" />
        </StackPanel>
    </DataTemplate>
    
    public class Message
    {
        public string Title { get; set; }
    }
    
    public class MessageWithProgress : Message
    {
        public double Progress { get; set; }
    }
    

    不过,您可以通过使用DataTemplateSelector 来避免在此示例中使用绑定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多