0

I'm trying to change properties from a user control, And I would like to these properties to reflect in UI. But it's not working

Here is my class.

public class Note
{
    public string Paper = "#FFFFFF";
    public string Text = "";
}

Here is my control

<UserControl
    x:Class="App.Controls.NoteControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="200"
    d:DesignWidth="200">

    <Grid Background="{Binding Path = Note.Paper, UpdateSourceTrigger=PropertyChanged}">
         <!-- ... -->
    </Grid>
</UserControl>

The control has a public Note property

public sealed partial class NoteControl : UserControl {

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

   public Note Note {
       get { return (Note)GetValue(NoteProperty); }
       set { SetValue(NoteProperty, value); }
   }

   public static readonly DependencyProperty NoteProperty =
        DependencyProperty.Register("Note", typeof(Note), typeof(NoteControl), null);

}

In my MainWindow there is a simple code to update the Note Property in my control.

MyNoteControl.Note = SomeNewNote;

What should I do in order to update the control's binding when changing the Note property?

Or maybe, there is another solution that can better address this issue?

Thanks

Daniel Santos
  • 14,328
  • 21
  • 91
  • 174
  • 2
    WPF data binding works with public properties only. Change your fields to properties like `public string Paper { get; set; } = "#FFFFFF";` Besides that, `UpdateSourceTrigger=PropertyChanged` has no effect in a one-way binding (as yours here). – Clemens Nov 14 '16 at 12:59
  • That's works great! @Clemens – Daniel Santos Nov 14 '16 at 13:04
  • 1
    Note also that there is no property change notification mechanism in place for the properties in your Note class. So either implement INotifyPropertyChanged, or make the properties read-only. – Clemens Nov 14 '16 at 13:06
  • Sorry, I don't understand your point. Which class should have the NotifyPropertyChanged implemented: Note, MainWindow or Control? Can you point me out an exemple? (you may edit my questions, if you like) – Daniel Santos Nov 14 '16 at 13:14
  • 1
    The Note class. Currently, if you set the Paper property of the current Note instance, the UI won't update. – Clemens Nov 14 '16 at 13:15

0 Answers0