0

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

4

1 回答 1

0

当我不确定是否存在特定属性时,我通常使用 Binding。例子:

<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避免在此示例中使用 Binding 。

于 2020-05-07T22:14:11.027 回答