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!!!
Solved! Go to Solution.
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)));
}
}
<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();
}
}
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)));
}
}
<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();
}
}
Thank you!!!!!!! This helped immensely!!!