Pederson
Pederson

Reputation: 3

WPF C# Binding Data from another Class

So I'm missing something simple or losing my mind. I am trying to reuse a class for multiple pages in a WPF application and bind the properties to the pages that instance it. I've tried setting the DataContext but I'm missing something. I'm loading the StockAnalysis page and then creating instance of the PriceChart class (this is the class for reuse) and I want the properties set in the PriceChart class to be the data to bind to the Stock.xaml.cs page. Even in setting the DataContext it is still looking for the StockAnalysis object. Why?

Stock.xaml.cs

 public partial class StockAnalysis : Page
 {
     PriceChart PChart = new PriceChart();

     public StockAnalysis()
     {
        InitializeComponent();

        //Load The Data
        List<Stock> HistoricalPrice = Database.GetPrices(ticker);

        //Create The Charts
        this.DataContext = PChart;
        PChart.ShowPriceChart(HistoricalPrice);
     }
 }

Stock.xaml (Look at the Last TexBlock for the Binding of "LastPrice")

    <Page x:Class="Stock.StockAnalysis"
          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" 
          xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
          xmlns:local="clr-namespace:Stock"
          mc:Ignorable="d" 
          d:DesignHeight="1000" d:DesignWidth="1200"
          Title="Stock Analysis">
                <StackPanel x:Name="LastClosePanel" Grid.Row="0" Grid.RowSpan="2" Grid.Column="5" Height="60" VerticalAlignment="Top" Margin="1,0,0,1" Style="{StaticResource LastCloseBackground}">
                    <TextBlock x:Name="LastCloseText" Foreground="OrangeRed" FontSize="12" HorizontalAlignment="Center" Margin="0,10,0,8">Last Close</TextBlock>
                    <TextBlock x:Name="LastCloseBind" Foreground="White" FontSize="16" HorizontalAlignment="Center" Text="{Binding LastPrice}"></TextBlock>
                </StackPanel>
   </Page>

PriceChart.cs (This is where I assign "LastPrice" in hopes to bind it to the TextBlock in stock.xaml.cs)

public class PriceChart
{
    public string LastPrice { get; set; }
    
    public void ShowPriceChart(List<Stock> FullList)
    {
        LastPrice = FullList[0].LastPrice.ToString("C");
        //DO OTHER THINGS
    }
}

Upvotes: 0

Views: 440

Answers (1)

Keith Stein
Keith Stein

Reputation: 6724

The problem is that PriceChart doesn't implement any change notification. With the current code, this is how things will go when StockAnalysis gets created:

  1. InitializeComponent() will create the TextBlocks and the binding. At this point, DataContext is null, so the binding will fail and the TextBlock stay empty.

  2. this.DataContext = PChart will trigger a binding update (because DataContext is a DependencyProperty, which means it does support change notification). When the binding updates, it will pull the value of LastPrice, which is currently still empty.

  3. ShowPriceChart will set the value of LastPrice, but because PriceChart doesn't support change notification, the binding doesn't know it needs to update, so the TextBlock stays empty.

To solve this, I would recomend your PriceChart implement the INotifyPropertyChanged interface per this article: How to: Implement Property Change Notification.

(Technically, moving PChart.ShowPriceChart(HistoricalPrice) before this.DataContext = PChart would also "solve" the problem, but only if you never need to update the bindings again after initialization.)

Upvotes: 1

Related Questions