Gustavo Gonçalves
Gustavo Gonçalves

Reputation: 539

Confused about Binding in UserControl

I'm creating a UserControl object, and i'm trying to assign values using Binding (with PropertyChanged). I made a prototype, when I assign value in ViewModel , the value does not appear or modify the UserControl component, but if I assign the value directly in the UserControl object that is in view, the modification works. I would like to understand what I'm doing wrong, since works fine if i just add an object on my window and binding directly (again, using PropertyChanged). Follow the code below.

Thanks for any help.

Best Regards,

Gustavo.

UserControl:

<UserControl x:Class="WpfControlLibrary1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="30" d:DesignWidth="300">
<Grid>
    <TextBlock Text="{Binding Title}" />
</Grid>
</UserControl>

Code Behind of User Control:

    public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    #region Title Property

    public static String GetTitle(DependencyObject obj)
    {
        return (String)obj.GetValue(TitleProperty);
    }

    public static void SetTitle(DependencyObject obj, String value)
    {
        obj.SetValue(TitleProperty, value);
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.RegisterAttached(
            "Title",
            typeof(String),
            typeof(UserControl1),
            new FrameworkPropertyMetadata(TitleChangedCallback)
            );

    private static void TitleChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        UserControl1 _this = (d as UserControl1);
    }

    private String title;
    public String Title
    {
        get { return title; }
        set
        {
            title = value;
            OnPropertyChanged("Title");
        }
    }

    #endregion

    #region INotifyPropertyChanged event and method

    public event PropertyChangedEventHandler PropertyChanged;

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    #endregion

** My Window: **

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:uc="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <uc:UserControl1 Title="{Binding TitleVM, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>

** My Code-Behind/ViewModel: **

public partial class MainWindow : INotifyPropertyChanged
{
    // Notify WPF that Counter changed
    public event PropertyChangedEventHandler PropertyChanged;

    public MainWindow()
    {
        InitializeComponent();

        TitleVM = "açsldkfjasçldkfj";
    }

    private String titleVM;
    public String TitleVM
    {
        get { return titleVM; }
        set
        {
            titleVM = value;
            OnPropertyChanged("TitleVM");
        }
    }

    // Create the OnPropertyChanged method to raise the event 
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

}

Upvotes: 0

Views: 221

Answers (2)

Gustavo Gon&#231;alves
Gustavo Gon&#231;alves

Reputation: 539

With help of HighCore, i've found one problem, and the other problem was my UserControl doesn't have a name defined.

Just put:

x:Name="UserControl"

Into XAML of UserControl and will work. Also, i've modified my code behind to this:

        public String Title
    {
        get { return (String)base.GetValue(TitleProperty); }
        set { base.SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty TitleProperty =
     DependencyProperty.RegisterAttached(
         "Title",
         typeof(String),
         typeof(UserControl1),
         new FrameworkPropertyMetadata(null)
         );

A clean code to our eyes.

PS: There's no need to put:

DataContext = this;

On Code Behind of UserControl. At least here only result on bug, no values was comming.

Thanks HighCore for you help.

Upvotes: 0

Fede
Fede

Reputation: 44038

Your Window doesnt have a DataContext. Bindings cannot be resolved.

Try this:

public MainWindow()
{
    InitializeComponent();

    DataContext = this; //This is what you're missing!

    TitleVM = "açsldkfjasçldkfj";
}

Edit:

You're also missing the same thing in the UserControl

public UserControl1()
{
    InitializeComponent();
    DataContext = this;
}

Upvotes: 2

Related Questions