0

so , i am new to the development field..

In my wpf, I'm trying to use, "PriceWPF" value from CandelClass

I've been plowing around, I don't understand my mistake...

I feel like the problem lies in datacontext

Here is a sample of the code

MainWindows.xaml.cs


namespace Wpf_bot {
  public partial class MainWindow: Window {
    public MainWindow() {
      InitializeComponent();
      this.DataContext = new CandelCheckClass();
      CandelCheckClass.KlineInfo();
    }
  }
}

Model.cs

using System.Text;
using System.ComponentModel;
using System.IO;

namespace Wpf_bot
{
public class CandelCheckClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

            public decimal PriceWPF { get; set; }  
    
        public static void KlineInfo()
        {
            var dd = new CandelCheckClass();
    
            dd.PriceWPF = 10;
        }  
    }

}

MainWindow.xaml

<Window x:Class="Wpf_bot.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:fc="clr-namespace:FancyCandles;assembly=FancyCandles"
Title="MainWindow" Height="600" Width="1100"\>

    <Grid x:Name="Layout" Margin="10,432,0,10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0*" />
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <fc:CandleChart CandlesSource="{Binding Path=.}" Margin="10,-400,0,20" HorizontalAlignment="Left" Width="800" Grid.ColumnSpan="2" />
    
        <TextBox Name="priceBox" IsReadOnly="True" Text="{Binding PriceWPF , StringFormat=Price : {0} BUSD}" Width="150" HorizontalAlignment="Left"  Background="#FFFFDBDB" TextAlignment="Center" BorderBrush="#FFBFA5A2" Margin="10,10,0,20" FontWeight="Bold" FontFamily="Calibri" FontSize="14" TextWrapping="Wrap" Grid.Column="1" Grid.Row="1" Height="40"/>       
        <Grid.RowDefinitions>
            <RowDefinition Height="0*" />
            <RowDefinition />
            <RowDefinition Height="0*"/>
        </Grid.RowDefinitions>
    
    
    </Grid>

\</Window\>
Jim my
  • 1
  • 1
  • 1
  • It's not clear to me what the purpose of the `dd` variable is. Why are you creating a new instance of `CandelCheckClass` within `CandelCheckClass`? Nothing is ever done with `dd`. What are you expecting to happen and why? – David Apr 04 '22 at 16:47
  • 2
    I think you should go and learn a bit about programming with objects, what an instance of a class is and what the `static` keyword is for. You are creating separate instances and expecting a property value change in one to be reflected in another. That's just not how this works. – Charles Mager Apr 04 '22 at 16:48
  • Jim my, it's unclear what your question is, are asking why is your UI not updating when you set PriceWPF to 10? Because the reason why is not your DataContext. The problem is because you never notify the UI by invoking the PropertyChanged event after you change the value. – Tam Bui Apr 04 '22 at 16:53
  • @TamBui the most fundamental reason is the instance set to 10 is not the one used by the window. – Charles Mager Apr 04 '22 at 17:02
  • @Charles Mager, you are correct. I should have said "the reason why is not *just* your DataContext". Both are a problem, but the one you identified is more egregious. – Tam Bui Apr 04 '22 at 17:06
  • @Charles Magger This is what I understood. I had created a static to allow a call from the main. Indeed, it doesn't set anything, I removed all that – Jim my Apr 04 '22 at 17:07
  • @, ok I'll look this way – Jim my Apr 04 '22 at 17:08
  • You're not fully implementing the INotifyPropertyChanged interface - you need to trigger that event when the property changes inside `CandelCheckClass`. – Steve Apr 04 '22 at 21:14

0 Answers0