【发布时间】:2010-03-06 04:09:04
【问题描述】:
我在用户控件中有一个图表。由于我想在应用程序中使用多个图表,我需要为用户控件的每个实例设置诸如系列标题之类的东西。到目前为止它工作正常,但我无法将变量(例如 seriesTitle)绑定到用户控件 xaml 中的目标。
请看以下代码:
像这样在 MainPage.xaml 中使用用户控件:
<local:ColumnInputChart
Name="Chart1"
seriesTitle="Diuretics"
seriesItemId="1"
getServiceUrl="/Service/MedIntake/1"
postServiceUrl="/Service/MedIntake/"
/>
例如在哪里seriesTitle 是用户控件中的公共变量。
public partial class ColumnInputChart : UserControl
{
public String seriesItemId { get; set; }
public String getServiceUrl { get; set; }
public String postServiceUrl { get; set; }
public String seriesTitle { get; set; }
在 ColumnInputChart.xaml 中,我正在尝试将 seriesTitle 绑定到系列的标题,但这不起作用。图例的标题为空。我需要以编程方式设置标题吗?
<chartingToolkit:AreaSeries ItemsSource="{Binding}"
DependentValuePath="Dpd"
IndependentValuePath="Date"
Title="{Binding seriesTitle, Mode=TwoWay}" />
我还意识到,在 MainPage.xaml 中设置的我的用户控件的自定义公共变量在用户控件的构造过程中尚未设置。但是一旦触发 Page_Loaded 事件,它们就可用。
请查看代码中的代码和 cmets 以阐明我的意思:
public String seriesItemId { get; set; }
public String getServiceUrl { get; set; }
public String postServiceUrl { get; set; }
public String seriesTitle { get; set; }
public ColumnInputChart()
{
// seriesItemId and so on are null here.
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
// and null here.
}
void Page_Loaded(object sender, RoutedEventArgs e) {
// seriesItemId, seriesTitle ... have now the
// value form MainPage.xaml
}
这是我的用户控件的图表代码:
<chartingToolkit:Chart Height="160" x:Name="Chart" BorderThickness="0" Style="{StaticResource MyChartStyle}">
<chartingToolkit:Chart.Axes>
<chartingToolkit:DateTimeAxis Orientation="X">
<chartingToolkit:DateTimeAxis.AxisLabelStyle>
<Style TargetType="chartingToolkit:DateTimeAxisLabel">
<Setter Property="StringFormat" Value="{}{0:d/M/yyyy}"/>
</Style>
</chartingToolkit:DateTimeAxis.AxisLabelStyle>
</chartingToolkit:DateTimeAxis>
<chartingToolkit:LinearAxis Orientation="Y" Location="Left" Title="Intake per day" ShowGridLines="True" />
</chartingToolkit:Chart.Axes>
<chartingToolkit:AreaSeries x:Name="MySeries2" ItemsSource="{Binding}"
DependentValuePath="Dpd"
IndependentValuePath="Date" Title="{Binding seriesTitle, Mode=TwoWay}" />
<chartingToolkit:ColumnSeries ItemsSource="{Binding}"
DependentValuePath="Dpd"
IndependentValuePath="Date"
IsSelectionEnabled="True"
Title="Missing Values"
SelectionChanged="ColumnSeries_SelectionChanged" />
</chartingToolkit:Chart>
我使用 ObservableCollections 作为图表的 ItemSource。
medIntakes = new ObservableCollection<MedIntakeObj>();
medIntakeBars = new ObservableCollection<MedIntakeObj>();
((AreaSeries)Chart.Series[0]).ItemsSource = medIntakes;
((ColumnSeries)Chart.Series[1]).ItemsSource = medIntakeBars;
感谢您的帮助!
【问题讨论】:
-
您安装了什么版本的工具包。 Title 属性未绑定是在更高版本中修复的错误(我认为是 10 月 9 日)。
-
你为图表分配了什么数据上下文?
-
据我所知,我已经安装了最新版本的工具包。现在是 2009 年 11 月。
-
我现在在图表中使用两个数据系列。两者都绑定到 ObservableCollection。使用 JSON 从 ASP.Net MVC 后端检索内容获取请求使用普通 POST 将更改发布回后端...工作正常。我会发布一些代码。
-
因此,如果我的理解正确,您并没有为
DataContext属性分配任何东西,因此您的绑定不起作用也就不足为奇了。
标签: c# silverlight data-binding xaml charts