如果我取出启用 wpf 实时图表的代码,那么我的数据绑定代码可以正常工作,但是当显示图表时,到我的标签的数据绑定不再显示数据字符串。
我使用 INotifyPropertyChanged 事件,我相信当我将数据上下文切换到图表时,就会出现问题。
我在一个单独的文件中有一个静态类来存储我的数据,我已将数据上下文包含在该本地类的 XAML 中,因此我可以从 XAML 中看到我的变量,这很好。
我显然在这里做错了什么。
`
<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>
`
如果我在创建图表数据后删除下面的行
this.DataContext = this;
然后我收到图表错误。
this.DataContext = this;覆盖 wpf 中设置的数据上下文。将您的代码放在一个类中