I have been using DataVisualization.Charting.Chart to produce few charts. Everything works very well, until when I put the same inside a DataTemplate of a ListBox.
Inside the ListBox, the Chart data shows up properly, but the Legends are not showing.
I have been using Normal LineSeries chart. Here is the code :
<ListBox Style="{DynamicResource listBasic}"
FontSize="12"
FontWeight="Normal"
ItemsSource="{Binding PsychrometricLogs}">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Name}"
FontFamily="Calibri"
FontSize="18"
Foreground="{DynamicResource defForegroundBrush}"
IsExpanded="True">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Border CornerRadius="5"
开发者_C百科 Margin="2,5,2,5"
Opacity=".3"
Background="#000000" />
<DockPanel>
<chart:Chart Title="{Binding DisplayName}"
FontSize="14"
DockPanel.Dock="Top"
LegendTitle="Legends"
Margin="20,15,20,5"
Foreground="#000000"
DataContext="{Binding Logs}">
<chart:LineSeries Title="Temperature"
AnimationSequence="FirstToLast"
IndependentValueBinding="{Binding TimeStamp}"
DependentValueBinding="{Binding Temparature}"
ItemsSource="{Binding}" />
<chart:LineSeries Title="Relative Humidity"
AnimationSequence="FirstToLast"
IndependentValueBinding="{Binding TimeStamp}"
DependentValueBinding="{Binding RelativeHumidity}"
ItemsSource="{Binding}" />
<chart:LineSeries Title="Grains Per Pound"
AnimationSequence="FirstToLast"
IndependentValueBinding="{Binding TimeStamp}"
DependentValueBinding="{Binding GrainsPerPound}"
ItemsSource="{Binding}" />
<chart:LineSeries Title="Grains Depression"
AnimationSequence="FirstToLast"
IndependentValueBinding="{Binding TimeStamp}"
DependentValueBinding="{Binding GrainsDepression}"
ItemsSource="{Binding}" />
</chart:Chart>
<ListView ItemsSource="{Binding Logs}"
DockPanel.Dock="Top"
Margin="5,0,5,5"
Padding="0">
<ListView.View>
<GridView AllowsColumnReorder="True">
<GridViewColumn Header="Time Stamp"
DisplayMemberBinding="{Binding TimeStamp}" />
<GridViewColumn Header="Temperature"
DisplayMemberBinding="{Binding Temparature}" />
<GridViewColumn Header="RH"
DisplayMemberBinding="{Binding RelativeHumidity}" />
<GridViewColumn Header="GPP"
DisplayMemberBinding="{Binding GrainsPerPound}" />
<GridViewColumn Header="GD"
DisplayMemberBinding="{Binding GrainsDepression}" />
</GridView>
</ListView.View>
</ListView>
</DockPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You can see, I have placed one ListView and a Chart. The ListView shows data properly as well as the chart. But the Legends does not show up.
I have already tried to put the same chart outside the ListBox and it shows up Legends correctly but not inside the DataTemplate.
It would be helpful if somebody tell me where I am wrong in this.
I suspect the problem is in your listBasic style. Your XAML works perfectly for me. I just pasted it into a new window and added the following in InitializeComponent():
var rand = new Random();
DataContext = new { PsychrometricLogs =
from i in Enumerable.Range(0, 5)
select new
{
Logs =
from j in Enumerable.Range(0, 10)
select new
{
TimeStamp = rand.Next(10),
Temparature = (decimal)rand.Next(100),
RelativeHumidity = (decimal)rand.Next(100),
GrainsPerPound = (decimal)rand.Next(10),
GrainsDepression = (decimal)rand.Next(10),
}
}};
For additional help, please show us the styles you are using and let us know your WPFToolkit version.
By the way, you mis-spelled "Temperature".
精彩评论