UWP property changed based on user property

I am using a custom control by binding values ​​from dependency properties. But the problem is, when I try to change the value, the UI is not updated. my XAML code in usercontrol:

<TextBox Text="{x:Bind Text}" />

      

and C #

 public event PropertyChangedEventHandler PropertyChanged;


    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value);
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs("Text"));
            }
        }
    }

    // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(Control1), new PropertyMetadata(null));

      

When I want to use it:

        <local:Control text="{Binding Val,Mode=TwoWay}" />

      

my ViewModel:

public event PropertyChangedEventHandler PropertyChanged;
    private string _val;
    public string Val
    {
        get
        {
            return _val;
        }

        set
        {

            if (_val != value)
            {_val = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this,
                        new PropertyChangedEventArgs("Val"));
                }
            }
        }

    }

      

And when I want to use it in my view and bind it to the view model, the property changes are not applied in the custom control.

+3


source to share


1 answer


The x:bind

default mode is used once, that you must change the mode to OneWay.

<TextBox Text="{x:Bind Text,Mode=OneWay}" />

      



You should not write code in SetValue(TextProperty, value);

and should not use the Notify property on the dependency property to change the automatic dependency change.

See: Dependency Properties

+4


source







All Articles