开发者

How to dynamically change DataPointStyle on wpf line chart from dataGrid_SelectedCellsChanged event

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 definit开发者_如何学编程ion 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜