如何从 dataGrid_SelectedCellsChanged 事件动态更改 wpf 折线图上的 DataPointStyle
我正在使用 WPFToolkit 创建具有多个 LineSeries(多种颜色)的折线图。我有一个 DataGrid 控件,其中 DataGrid.RowCount = LineSeriesCount。数据网格显示图表中每条线的相关数据。
当我在 dataGrid_SelectedCellsChanged 上选择相应的数据网格时,我需要突出显示(将其颜色更改为黑色)图表中的线条。
我尝试了以下链接,但没有帮助:在 WPF 中动态更改样式
所以 var dpStyle = new Style() { BasedOn = originalStyle };不工作。
我使用下面的代码来创建必要的样式:
public Window1()
{
InitializeComponent();
dataGrid1.Width = mcChart.Width;
HighlightedBlackDataPointStyle = new Style(typeof(LineDataPoint));
HighlightedBlackDataPointStyle.Setters.Add(new Setter(LineDataPoint.VisibilityProperty, Visibility.Hidden)); HighlightedBlackDataPointStyle.Setters.Add(new Setter(LineDataPoint.OpacityProperty, 0.01)); HighlightedBlackDataPointStyle.Setters.Add(new Setter(LineDataPoint.BackgroundProperty, new SolidColorBrush(Colors.Black)));
this.Resources.Add("HighlightedBlackDataPointStyle", HighlightedBlackDataPointStyle);
}
这就是我尝试设置上述样式的方式:
private void dataGrid1_SelectedCellsChanged(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
{
if (dataGrid1.SelectedIndex >= 0)
{
mcChart.Series.OfType<LineSeries>().ElementAt(dataGrid1.SelectedIndex).DataPointStyle = HighlightedBlackDataPointStyle;
mcChart.Series.OfType<LineSeries>().ElementAt(dataGrid1.SelectedIndex).Refresh();
mcChart.Series.OfType<LineSeries>().ElementAt(dataGrid1.SelectedIndex).UpdateLayout();
mcChart.UpdateLayout();
}
}
这会将图例的颜色更改为黑色,但不是预期的线条。
我通过在创建时设置 Sytle 来在 C# 代码隐藏中添加了所有 lineSeries。我在 XAML 中没有任何样式定义。因此,不确定是否可以使用 XAML 中的 DynamicResource Binding。
请让我知道如何通过 C# 代码而不是 XAML 实现此目的。
i'm using the WPFToolkit to create a line chart with multiple LineSeries(multiple colors). I have a DataGrid control where DataGrid.RowCount = LineSeriesCount. The datagrid displays relevant data for each line in the chart.
I need to highlight(change its color to Black) the line in the chart when i select the respective datagrid upon dataGrid_SelectedCellsChanged.
i tried the below link but it didn't help: Change a style dynamically in WPF
So var dpStyle = new Style() { BasedOn = originalStyle }; is not working.
I'm using below code to create the necessary Style:
public Window1()
{
InitializeComponent();
dataGrid1.Width = mcChart.Width;
HighlightedBlackDataPointStyle = new Style(typeof(LineDataPoint));
HighlightedBlackDataPointStyle.Setters.Add(new Setter(LineDataPoint.VisibilityProperty, Visibility.Hidden)); HighlightedBlackDataPointStyle.Setters.Add(new Setter(LineDataPoint.OpacityProperty, 0.01)); HighlightedBlackDataPointStyle.Setters.Add(new Setter(LineDataPoint.BackgroundProperty, new SolidColorBrush(Colors.Black)));
this.Resources.Add("HighlightedBlackDataPointStyle", HighlightedBlackDataPointStyle);
}
This is how i'm trying to set the above Style:
private void dataGrid1_SelectedCellsChanged(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
{
if (dataGrid1.SelectedIndex >= 0)
{
mcChart.Series.OfType<LineSeries>().ElementAt(dataGrid1.SelectedIndex).DataPointStyle = HighlightedBlackDataPointStyle;
mcChart.Series.OfType<LineSeries>().ElementAt(dataGrid1.SelectedIndex).Refresh();
mcChart.Series.OfType<LineSeries>().ElementAt(dataGrid1.SelectedIndex).UpdateLayout();
mcChart.UpdateLayout();
}
}
This is changing the color of Legend to black but not the intended line.
I've added all the lineSeries in C# codebehind by setting the Sytle while creating it. I do not have any Style definition in XAML. So, not sure if i can use the DynamicResource Binding from XAML.
Please let me know how can i achieve this from C# code behind rather than XAML.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题相当过时,但我会回答,因为也许有人有同样的问题。
事实上,这并不像看起来那么困难。
要更改线条的颜色,只需使用
LineSeries
的Background
属性:我使用
Tag
属性,因为可以通过以下方式更改项目的行索引排序,而 Id 始终相同。我还没有发布
ItemViewModel
的代码和DataGrid
的 xaml,但我认为主要思想很清楚。The question is rather outdated, but I will answer because maybe someone has the same problem.
Actually it is not as difficult as it seems.
To change the color of line just use the
Background
property ofLineSeries
:I use the
Tag
property because the row index of item can be changed by sorting, whereas Id is always the same.I haven't posted the code of the
ItemViewModel
and the xaml of theDataGrid
, but I think that the main idea is clear.