Databinding stops working while Live Charts databinding is enabled

44 Views Asked by At

If I takeout the code which enables the wpf Live Chart then my databinding code works fine but when the chart is displayed the databinding to my labels no longer displays the data strings.

I use the INotifyPropertyChanged event, I believe it is when I switch the data context to the chart is where it goes wrong.

I have a static class in a separate file that stores my data, I have included the data context in my XAML for that local class and so I can see my variable from XAML which is fine.

I am clearly doing something wrong here.

` <Window.DataContext> local:ComConnection/ </Window.DataContext>

    <Grid x:Name="Grid">
    <DockPanel x:Name="dp_Main" Background="#FF969696" LastChildFill="true" Width="Auto" DockPanel.Dock="Top">
        <Grid DockPanel.Dock="Top" Margin="1,1,1,1" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="350"/>
                <ColumnDefinition Width="10"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
            </Grid.RowDefinitions>
            <TabControl x:Name="tab_Hardware" Margin="2,2,2,2" >
                <TabItem Header="Finger Assembly Hardware" FontFamily="Segoe UI" FontSize="20">
                    <Grid Margin="1,1,1,1" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="50"/>
                        </Grid.RowDefinitions>
                        <!-- TEST PARAMETERS -->
                        <GroupBox x:Name="gb_HwParams"  Header="HARDWARE PARAMETERS" Margin="10,0,10,5" FontWeight="Bold" BorderBrush="Black" FontSize="16">
                            <Grid x:Name="Meter_Grid" Width="Auto" Height="Auto" Visibility="Visible" MinWidth="150" Margin="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="5*"/>
                                    <ColumnDefinition Width="5*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="5" />
                                    <RowDefinition Height="26" />
                                    <RowDefinition Height="26" />
                                    <RowDefinition Height="26" />
                                    <RowDefinition Height="26" />
                                    <RowDefinition Height="5" />
                                </Grid.RowDefinitions>
                                <!-- HARDWARE -->
                                <Label Grid.Column="0" Content="Hardware:" FontSize="16" FontWeight="Normal" HorizontalContentAlignment="Right" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1" Padding="5,0,5,0" />
                                <Label Name="lbl_HwRev" Grid.Column="1" Content="{Binding Path=Hardware, UpdateSourceTrigger=PropertyChanged}" FontSize="16" FontWeight="Bold" HorizontalContentAlignment="Center" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1" Padding="5,0,5,0" />

                                <Label Grid.Column="0" Content="Firmware:" FontSize="16" FontWeight="Normal" HorizontalContentAlignment="Right" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="2" Padding="5,0,5,0" />
                                <Label Name="lbl_FwRev" Grid.Column="1" Content="N/A" FontSize="16" FontWeight="Bold" HorizontalContentAlignment="Left" VerticalContentAlignment="Stretch" HorizontalAlignment="Center" Grid.Row="2" Padding="5,0,5,0" />

                                <Label Grid.Column="0" Content="Status:" FontSize="16" FontWeight="Normal" HorizontalContentAlignment="Right" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="3" Padding="5,0,5,0" />
                                <Label Name="lbl_Status" Grid.Column="1" Content="N/A" FontSize="16" FontWeight="Bold" HorizontalContentAlignment="Left" VerticalContentAlignment="Stretch" HorizontalAlignment="Center" Grid.Row="3" Padding="5,0,5,0" />

                                <Label Grid.Column="0" Content="Serial:" FontSize="16" FontWeight="Normal" HorizontalContentAlignment="Right" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="4" Padding="5,0,5,0" />
                                <Label Name="lbl_Serial" Grid.Column="1" Content="N/A" FontSize="16" FontWeight="Bold" HorizontalContentAlignment="Left" VerticalContentAlignment="Stretch" HorizontalAlignment="Center" Grid.Row="4" Padding="5,0,5,0" />
                            </Grid>
                        </GroupBox>
                    </Grid>
                </TabItem>
            </TabControl>
            <TabControl x:Name="tab_DeviceData"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="2">
                <TabItem Header="Graph Data" FontFamily="Segoe UI" FontSize="20">
                    <Grid>
                        <lvc:CartesianChart Name="lc_MainChart" Series="{Binding ChartSeries}" AxisY="{Binding AxisYCollection}" Hoverable="False" Grid.Row="0" Grid.Column="1" DisableAnimations="True" LegendLocation="Right" Margin="2,2,2,2" BorderBrush="Black" BorderThickness="2,2,2,2" >
                            <lvc:CartesianChart.AxisX>
                                <lvc:Axis Title="DATA POINTS" FontSize="18" Labels="{Binding Labels}">
                                    <lvc:Axis.Separator>
                                        <lvc:Separator IsEnabled="False" Step="50"></lvc:Separator>
                                    </lvc:Axis.Separator>
                                </lvc:Axis>
                            </lvc:CartesianChart.AxisX>
                        </lvc:CartesianChart>
                    </Grid>
                </TabItem>
            </TabControl>
        </Grid>
    </DockPanel>
</Grid>
</Window>

`

If I remove the line below after creating my chart data

this.DataContext = this;

Then I get a chart error.

1

There are 1 best solutions below

0
On

this.DataContext = this; overwrites your data context set in wpf. Put your code in one class