如何从 dataGrid_SelectedCellsChanged 事件动态更改 wpf 折线图上的 DataPointStyle

发布于 2024-10-21 04:28:30 字数 1917 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滴情不沾 2024-10-28 04:28:30

这个问题相当过时,但我会回答,因为也许有人有同样的问题。

事实上,这并不像看起来那么困难。
要更改线条的颜色,只需使用 LineSeriesBackground 属性:

    private Brush previousColor;

    private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.RemovedItems != null && e.RemovedItems.Count > 0)
        {
            var removedItem = (ItemViewModel)e.RemovedItems[0];
            var line = chart.Series.OfType<LineSeries>().FirstOrDefault(ls => (int)ls.Tag == removedItem.Id);

            line.Background = previousColor;
        }
        if (e.AddedItems != null && e.AddedItems.Count > 0)
        {
            var selectedItem = (ItemViewModel)e.AddedItems[0];
            var line = chart.Series.OfType<LineSeries>().FirstOrDefault(ls => (int)ls.Tag == selectedItem.Id);

            previousColor = line.Background;
            line.Background = Brushes.Black;
        }
    }

我使用 Tag 属性,因为可以通过以下方式更改项目的行索引排序,而 Id 始终相同。

<charting:Chart x:Name="chart" DataContext="{Binding Items}" Grid.Row="1" Grid.Column="1">
        <charting:Chart.Series>
            <charting:LineSeries DataContext="{Binding [0]}" ItemsSource="{Binding ChartItems}" Title="{Binding Title}" Tag="{Binding Id}" 
                                 IndependentValuePath="XValue"  DependentValuePath="YValue" />

我还没有发布 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 of LineSeries:

    private Brush previousColor;

    private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.RemovedItems != null && e.RemovedItems.Count > 0)
        {
            var removedItem = (ItemViewModel)e.RemovedItems[0];
            var line = chart.Series.OfType<LineSeries>().FirstOrDefault(ls => (int)ls.Tag == removedItem.Id);

            line.Background = previousColor;
        }
        if (e.AddedItems != null && e.AddedItems.Count > 0)
        {
            var selectedItem = (ItemViewModel)e.AddedItems[0];
            var line = chart.Series.OfType<LineSeries>().FirstOrDefault(ls => (int)ls.Tag == selectedItem.Id);

            previousColor = line.Background;
            line.Background = Brushes.Black;
        }
    }

I use the Tag property because the row index of item can be changed by sorting, whereas Id is always the same.

<charting:Chart x:Name="chart" DataContext="{Binding Items}" Grid.Row="1" Grid.Column="1">
        <charting:Chart.Series>
            <charting:LineSeries DataContext="{Binding [0]}" ItemsSource="{Binding ChartItems}" Title="{Binding Title}" Tag="{Binding Id}" 
                                 IndependentValuePath="XValue"  DependentValuePath="YValue" />

I haven't posted the code of the ItemViewModel and the xaml of the DataGrid, but I think that the main idea is clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文