Skip to content

Add support for x:Bind Functions #347

Closed

Description

@Henk7

As documented at https://docs.microsoft.com/en-us/windows/uwp/data-binding/function-bindings
Functions in x:Bind would be a real boon because it is the only way in UWP to access static Properties or Functions.

Example:

            <TextBlock Text="{x:Bind UI:Translations.TestMe}"/>

Activity

  1. peternary commented on Aug 6, 2019

    @peternary

    StaticBindSample.zip

    Attached is a sample of how we wish to use x:Bind to assign resource values to xaml controls. Functionality is present in UWP, but not in any other platform.

  2. Massimo37 commented on Dec 9, 2019

    @Massimo37
    Contributor

    A quick summary of how x:Bind is handled in UWP:..


    • Function binding using a function from the code-behind:

    <DataTemplate x:DataType="local:ColorEntry">
        <Grid Background="{x:Bind local:ColorEntry.Brushify(Color), Mode=OneWay}" Width="240">
            <TextBlock Text="{x:Bind ColorName}" Foreground="{x:Bind TextColor(Color)}" Margin="10,5" />
        </Grid>
    </DataTemplate>
    class ColorEntry
    {
        public string ColorName { get; set; }
        public Color Color { get; set; }
    
        public static SolidColorBrush Brushify(Color c)
        {
            return new SolidColorBrush(c);
        }
    
        public SolidColorBrush TextColor(Color c)
        {
            return new SolidColorBrush(((c.R * 0.299 + c.G * 0.587 + c.B * 0.114) > 150) ? Colors.Black : Colors.White);
        }
    }
    • Function binding using a static function:

    <Page 
         xmlns:local="using:MyNamespace">
         ...
        <StackPanel>
            <TextBlock x:Name="BigTextBlock" FontSize="20" Text="Big text" />
            <TextBlock FontSize="{x:Bind local:MyHelpers.Half(BigTextBlock.FontSize)}" 
                       Text="Small text" />
        </StackPanel>
    </Page>
    namespace MyNamespace
    {
        static public class MyHelpers
        {
            public static double Half(double value) => value / 2.0;
        }
    }
    • Function binding using a static system function:

    <Page 
         xmlns:sys="using:System"
         xmlns:local="using:MyNamespace">
         ...
         <CalendarDatePicker Date="{x:Bind sys:DateTime.Parse(TextBlock1.Text)}" />
         <TextBlock Text="{x:Bind sys:String.Format('{0} is now available in {1}', local:MyPage.personName, local:MyPage.location)}" />
    </Page>
    • Function binding using a two-way binding:

    <TextBlock Text="{x:Bind a.MyFunc(b), BindBack=a.MyFunc2, Mode=TwoWay}" />

    Important points with regards to how UWP handles x:Bind on functions:

    The function being bound to needs to:

    • Be accessible to the code and metadata – so internal / private work in C#
    • Overloading is based on the number of arguments, not type, and it will try to match to the first overload with that many arguments
    • The argument types need to match the data being passed in – we don’t do narrowing conversions
    • The return type of the function needs to match the type of the property that is using the binding

    Multiple function arguments can be specified, separated by comma's (,)

    • Binding Path – Same syntax as if you were binding directly to that object.
      • If the mode is OneWay/TwoWay then change detection will be performed and the binding re-evaluated upon object changes
    • Constant string enclosed in quotes – quotes are needed to designate it as a string. Hat (^) can be used to escape quotes in strings
    • Constant Number - for example -123.456
    • Boolean – specified as "x:True" or "x:False"

  3. added a commit that references this issue on Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions