Dynamic binding in c# where we cannot use Compiled data binding(x:bind)

96 views Asked by At

I am working on building a UWP application with c#, where in I found out about Compiled data binding(x:bind). It has many advantageous like performance, compile time errors in xaml page, binding events etc. When I go through one of the tutorial about compiled data binding, it clearly states we cannot use x:bind in scenarios, where we are binding objects dynamically. I am looking for an example, specifically for runtime binding which cannot be solved by using x:bind. A small piece of code with dynamic object binding will be much appreciated. Thanks in advance

1

There are 1 answers

0
Luandersonn Airton On

I usually use Binding when I'm not sure if a specific property exists. Example:

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

Still, you can avoid using Binding in this example by using DataTemplateSelector.