Select to view content in your preferred language

Binding to an object class property

142
2
Jump to solution
3 weeks ago
GaryBilotta
Regular Contributor

Hello.  I am trying to bind to a class property in a dock pane, but am having trouble doing so.  I can bind to a property on the dock pane, but not as part of a class object.  I have read many posts on the internet and it should not be that hard.  Here are snippets of what I currently have.

xaml:

d:DataContext="{Binding Path=ui.AddressEditorDockPaneViewModel}">

<TextBox Grid.Row="1" Grid.Column="0" MaxLength="5" x:Name="HseNo" Text="{Binding Path = pointAddress.HseNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="3,3,3,3" TextWrapping="Wrap" VerticalAlignment="Top" Width="50" BorderBrush="Black" BorderThickness=".75" Background="Transparent"  Tag=""/>

 

Back-end c# class in ViewModel (AddressEditorDockPaneViewModel) with HseNo property:

public class PointAddress : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler? PropertyChanged;

    private static string _HseNo;
    public string HseNo
    {
        get { return _HseNo; }
        //set { SetProperty(ref _HseNo, value); }
        set
        {
            if (value == _HseNo)
                return;
            _HseNo = value.ToString();
            
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(HseNo)));
        }
    }
}

 

Create a new PointAddress object (I have added this part in a couple of different forms (form level variable, and form level variable with creating new in the ShowAsync event):

public PointAddress pointAddress = new PointAddress();

 

Thank you for helping!!!

 

 

0 Kudos
1 Solution

Accepted Solutions
SumitMishra_016
Frequent Contributor

It looks like the issue lies in how the binding is set up and how the pointAddress object is exposed in your AddressEditorDockPaneViewModel. The key is to make sure that pointAddress is properly exposed as a property in the view model and is part of the data context hierarchy.

 

In your AddressEditorDockPaneViewModel, make sure pointAddress is a property and not just a field. For example:

 

public event PropertyChangedEventHandler? PropertyChanged;

    private PointAddress _pointAddress = new PointAddress();
    public PointAddress PointAddress
    {
        get { return _pointAddress; }
        set
        {
            if (_pointAddress == value)
                return;

            _pointAddress = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PointAddress)));
        }
    }

 

Update your TextBox binding to use the PointAddress property in the view model

 

<TextBox Grid.Row="1" Grid.Column="0" MaxLength="5" x:Name="HseNo" 
         Text="{Binding Path=PointAddress.HseNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
         HorizontalAlignment="Left" Margin="3,3,3,3" TextWrapping="Wrap" 
         VerticalAlignment="Top" Width="50" BorderBrush="Black" BorderThickness=".75" 
         Background="Transparent" Tag=""/>
​

 

 Please check this also in your xaml.cs , sometimes I also forget:

 

public partial class AddressEditorDockPane : UserControl
{
    public AddressEditorDockPane()
    {
        InitializeComponent();
        DataContext = new AddressEditorDockPaneViewModel();
    }
}

 

View solution in original post

0 Kudos
2 Replies
SumitMishra_016
Frequent Contributor

It looks like the issue lies in how the binding is set up and how the pointAddress object is exposed in your AddressEditorDockPaneViewModel. The key is to make sure that pointAddress is properly exposed as a property in the view model and is part of the data context hierarchy.

 

In your AddressEditorDockPaneViewModel, make sure pointAddress is a property and not just a field. For example:

 

public event PropertyChangedEventHandler? PropertyChanged;

    private PointAddress _pointAddress = new PointAddress();
    public PointAddress PointAddress
    {
        get { return _pointAddress; }
        set
        {
            if (_pointAddress == value)
                return;

            _pointAddress = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PointAddress)));
        }
    }

 

Update your TextBox binding to use the PointAddress property in the view model

 

<TextBox Grid.Row="1" Grid.Column="0" MaxLength="5" x:Name="HseNo" 
         Text="{Binding Path=PointAddress.HseNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
         HorizontalAlignment="Left" Margin="3,3,3,3" TextWrapping="Wrap" 
         VerticalAlignment="Top" Width="50" BorderBrush="Black" BorderThickness=".75" 
         Background="Transparent" Tag=""/>
​

 

 Please check this also in your xaml.cs , sometimes I also forget:

 

public partial class AddressEditorDockPane : UserControl
{
    public AddressEditorDockPane()
    {
        InitializeComponent();
        DataContext = new AddressEditorDockPaneViewModel();
    }
}

 

0 Kudos
GaryBilotta
Regular Contributor

Thank you!!!!!!!  This helped immensely!!!