Two way function bindings Some troubles

Mauro Fantina 106 Reputation points
2020-08-27T09:05:04.957+00:00

Hello,

I'm developing UWP application where I need to Bind muxc:NumberBox with ushort? property and I have some troubles:

I create simple code to test TwoWayBindings between two muxc:NumberBox sharing the same property.

c#

    public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private ushort? _a = 16;

        public ushort? a
        {
            get => _a;

            set
            {
                _a = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(a)));
            }
        }

        public MainPage()
        {
            this.InitializeComponent();
        }

        public double MyFunc(ushort? x) =>(double) x.Value;

        public ushort? MyFunc2(double x) => (ushort?)x;
    }

xaml

        <muxc:NumberBox Value="{x:Bind MyFunc(a), BindBack=MyFunc2, Mode=TwoWay}" Minimum="0" Maximum="65535"/>
        <muxc:NumberBox Value="{x:Bind MyFunc(a), BindBack=MyFunc2, Mode=TwoWay}"  Minimum="0" Maximum="65535"/>

But it doesn't work : xaml calls MyFunc2 but the returned value is not pushed on property "a"

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,321 Reputation points
    2020-08-27T15:49:36.187+00:00

    Hi Mauro,
    I change code in your functions and it works fine. Instead of NumberBox I used Slider:

    <Page  
        x:Class="App1.Page11"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        xmlns:local="using:App1"  
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
        mc:Ignorable="d"  
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
      <StackPanel>  
        <Slider Value="{x:Bind MyFunc(a), BindBack=MyFunc2, Mode=TwoWay}" Minimum="0" Maximum="65535"/>  
        <Slider Value="{x:Bind MyFunc(a), BindBack=MyFunc2, Mode=TwoWay}" Minimum="0" Maximum="65535"/>  
        <TextBlock Text="{Binding a}"/>  
      </StackPanel>  
    </Page>  
    

    And CodeBehind:

    20879-x.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.