How to binding an object property in a userControl?

0 Answers 57 Views
Window
Fenix
Top achievements
Rank 1
Fenix asked on 12 Jul 2023, 12:43 PM

Hi, i'm having a rough fight with WPF and its Binding system. I just wanna bind a property of an instance called HPP, this property is pressure and this object exist in MainViewModel, so in MainWindow.xaml a have this:


<componentes:SetComponent x:Name="HPP_Trip_Route" Grid.Row="1" Grid.Column="1" VerticalAlignment="Top" Title="TRIP" VariableValue="{Binding HPPRoute.Pressure}" Unity="PSI" Loaded="SetComponent_Loaded" FSizeTitle="24" FSizeVariable="36" FSizeUnity="14"/>
In SetComponent:
<TextBlock x:Name="ValueSetComponent" Grid.Row="1" Text="{Binding VariableValue, RelativeSource={RelativeSource AncestorType=UserControl}}" FontSize="{Binding FSizeVariable}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Foreground="#FFFFFF" Margin="0, 5, 0, 5 "/>
Dar clic y arrastrar para mover
In MainWindow.xaml.cs:

public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private readonly Variables GV = Variables.GetInstancia();
        private MainViewModel _mainViewModel;
        public MainViewModel MainViewModel
        {
            get { return _mainViewModel; }
            set
            {
                _mainViewModel = value;
                OnPropertyChanged();
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            GV.MainViewModel.StartAssign(GV);
            MainViewModel = GV.MainViewModel;
            GV.MainViewModel.MainWindow = this;
            DataContext = MainViewModel;
     
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

I tried with RelativeSource without success. If i create a variable in MainViewModel it works. Any help?

Dinko
Telerik team
commented on 14 Jul 2023, 07:25 AM

I made a project from the code you have given. It looks like everything is working fine. Try this attached project and tell me is it working properly and is the setup right.

No answers yet. Maybe you can help?

Tags
Window
Asked by
Fenix
Top achievements
Rank 1
Share this question
or