我一直在使用DataVisualization.Charting.Chart生成一些图表。一切都很好,直到我把它放在ListBox的DataTemplate中。
在ListBox中,图表数据正确显示,但图例未显示。
我一直在使用Normal LineSeries图表。这是代码:
<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"
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>
你可以看到,我已经放置了一个ListView和一个图表。 ListView正确显示数据以及图表。但是传说没有出现。
我已经尝试将相同的图表放在ListBox之外,它正确地显示了Legends但不在DataTemplate中。
如果有人告诉我这里错了,那会很有帮助。
答案 0 :(得分:3)
我怀疑问题出在你的listBasic风格中。您的XAML非常适合我。我只是将其粘贴到一个新窗口并在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),
}
}};
如需其他帮助,请向我们展示您使用的样式,并告诉我们您的WPFToolkit版本。
顺便说一句,你错误拼写了“温度”。