Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为wpf工具箱中图例项附近的矩形指定颜色_C#_Wpf_Charts_Legend_Wpftoolkit - Fatal编程技术网

C# 为wpf工具箱中图例项附近的矩形指定颜色

C# 为wpf工具箱中图例项附近的矩形指定颜色,c#,wpf,charts,legend,wpftoolkit,C#,Wpf,Charts,Legend,Wpftoolkit,我动态地向图表中添加lineseries,如下所示 foreach(yieldSeries中的KeyValuePair tempSeries) { LineSeries LineSeries=新的LineSeries(); lineSeries.DependentValuePath=“Value”; lineSeries.IndependentValuePath=“Key”; lineSeries.ItemsSource=tempSeries.Value; lineSeries.Title=t

我动态地向图表中添加lineseries,如下所示

foreach(yieldSeries中的KeyValuePair tempSeries)
{
LineSeries LineSeries=新的LineSeries();
lineSeries.DependentValuePath=“Value”;
lineSeries.IndependentValuePath=“Key”;
lineSeries.ItemsSource=tempSeries.Value;
lineSeries.Title=tempSeries.Key;
SetResourceReference(FrameworkElement.StyleProperty,“CommonLineSeries”);
lineSeries.Tag=画笔.Red;
lineSeries.Background=画笔.Red;
yieldTrendChart.Series.Add(lineSeries);

}
您需要编辑LegendItem样式以更改图例矩形的颜色

xaml

Window x:Class="WpfApplication8.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             
    xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:visualizationToolkit="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:datavis ="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="Window1" Height="500" Width="700">
<Window.Resources>
    <Style x:Key="LineSeriesStyle1" TargetType="{x:Type chartingToolkit:LineSeries}">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type chartingToolkit:LineSeries}">
                    <Canvas x:Name="PlotArea">
                        <Polyline Points="{TemplateBinding Points}" Stroke="{Binding Tag,RelativeSource={RelativeSource AncestorType={x:Type chartingToolkit:LineSeries}}}" />
                    </Canvas>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <chartingToolkit:Chart x:Name="mcChart"  >
        <chartingToolkit:LineSeries Tag="Green" x:Name="chart" DependentValuePath="Value"  IsSelectionEnabled="True" IndependentValuePath="Key" ItemsSource="{Binding}" Style="{StaticResource LineSeriesStyle1}">
            <chartingToolkit:LineSeries.LegendItemStyle>
                <Style TargetType="chartingToolkit:LegendItem" >
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type chartingToolkit:LegendItem}">
                                <Border BorderBrush="Black" BorderThickness="0">
                                    <StackPanel>
                                        <StackPanel Orientation="Horizontal" >
                                            <Rectangle Width="12" Height="12" Fill="{Binding ElementName=chart,Path=Tag}" StrokeThickness="1"  />
                                            <datavis:Title Content="{TemplateBinding Content}" Foreground="{Binding ElementName=chart,Path=Tag}" FontSize="18" Margin="10"/>
                                        </StackPanel>
                                    </StackPanel>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </chartingToolkit:LineSeries.LegendItemStyle>
        </chartingToolkit:LineSeries>
    </chartingToolkit:Chart>
</Grid>
{

公共部分类窗口1:窗口
{
公共窗口1()
{
初始化组件();
LoadColumnChartData();
}
私有void LoadColumnChartData()
{
((LineSeries)mcChart.Series[0])项目来源=
新的KeyValuePair[]{
新的KeyValuePair(“项目经理”,13),
新的KeyValuePair(“CEO”,23岁),};
}
}
}

结果

更新

 <Window.Resources>
    <Style x:Key="LineSeriesStyle1" TargetType="{x:Type chartingToolkit:LineSeries}">
        <Setter Property="Tag" Value="{Binding Tag,RelativeSource={RelativeSource AncestorType={x:Type chartingToolkit:LineSeries}}}"></Setter>
        <Setter Property="OverridesDefaultStyle" Value="True"></Setter>
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type chartingToolkit:LineSeries}">
                    <Canvas x:Name="PlotArea">
                        <Polyline x:Name="polyline"  Points="{TemplateBinding Points}" Stroke="{Binding Tag,RelativeSource={RelativeSource TemplatedParent}}" />
                    </Canvas>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</Window.Resources>
<Grid>
    <chartingToolkit:Chart Name="lineChart">
        <chartingToolkit:LineSeries Name="chart1" Tag="Blue" Background="Green"  Style="{StaticResource LineSeriesStyle1}" Title="KW Gastats"  DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding [0]}" IsSelectionEnabled="True">
            <chartingToolkit:LineSeries.LegendItemStyle>
                <Style TargetType="{x:Type chartingToolkit:LegendItem}" >
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type chartingToolkit:LegendItem}">
                                <Border >
                                    <StackPanel>
                                        <StackPanel Orientation="Horizontal" >
                                            <Rectangle Width="12" Height="12" Fill="{Binding ElementName=chart1,Path=Tag}"  Stroke="{Binding Background}" StrokeThickness="1"  />
                                            <datavis:Title Content="{TemplateBinding Content}" Foreground="{Binding ElementName=chart1,Path=Tag}" FontSize="18" Margin="10"/>
                                        </StackPanel>
                                    </StackPanel>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </chartingToolkit:LineSeries.LegendItemStyle>
        </chartingToolkit:LineSeries>
        <chartingToolkit:LineSeries Name="chart2" Tag="Green"   Style="{StaticResource LineSeriesStyle1}" Title="Preu KW"   DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding [1]}" IsSelectionEnabled="True" >
            <chartingToolkit:LineSeries.LegendItemStyle>
                <Style TargetType="{x:Type chartingToolkit:LegendItem}" >
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type chartingToolkit:LegendItem}">
                                <Border >
                                    <StackPanel>
                                        <StackPanel Orientation="Horizontal" >
                                            <Rectangle Width="12" Height="12" Fill="{Binding ElementName=chart2,Path=Tag}"   StrokeThickness="1"  />
                                            <datavis:Title Content="{TemplateBinding Content}" Foreground="{Binding ElementName=chart2,Path=Tag}" FontSize="18" Margin="10"/>
                                        </StackPanel>
                                    </StackPanel>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </chartingToolkit:LineSeries.LegendItemStyle>
        </chartingToolkit:LineSeries>
    </chartingToolkit:Chart>
</Grid>
InitializeComponent();

        List<KeyValuePair<DateTime, int>> llistaGastats = new List<KeyValuePair<DateTime, int>>();
        llistaGastats.Add(new KeyValuePair<DateTime, int>(DateTime.Now, 100));
        llistaGastats.Add(new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(1), 200));
        List<KeyValuePair<DateTime, int>> llistaPreu = new List<KeyValuePair<DateTime, int>>();
        llistaPreu.Add(new KeyValuePair<DateTime, int>(DateTime.Now, 300));
        llistaPreu.Add(new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(1), 300));
        var dataSourceList = new List<List<KeyValuePair<DateTime, int>>>();
        dataSourceList.Add(llistaGastats);
        dataSourceList.Add(llistaPreu);
        lineChart.DataContext = dataSourceList;

c#代码

 <Window.Resources>
    <Style x:Key="LineSeriesStyle1" TargetType="{x:Type chartingToolkit:LineSeries}">
        <Setter Property="Tag" Value="{Binding Tag,RelativeSource={RelativeSource AncestorType={x:Type chartingToolkit:LineSeries}}}"></Setter>
        <Setter Property="OverridesDefaultStyle" Value="True"></Setter>
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type chartingToolkit:LineSeries}">
                    <Canvas x:Name="PlotArea">
                        <Polyline x:Name="polyline"  Points="{TemplateBinding Points}" Stroke="{Binding Tag,RelativeSource={RelativeSource TemplatedParent}}" />
                    </Canvas>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</Window.Resources>
<Grid>
    <chartingToolkit:Chart Name="lineChart">
        <chartingToolkit:LineSeries Name="chart1" Tag="Blue" Background="Green"  Style="{StaticResource LineSeriesStyle1}" Title="KW Gastats"  DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding [0]}" IsSelectionEnabled="True">
            <chartingToolkit:LineSeries.LegendItemStyle>
                <Style TargetType="{x:Type chartingToolkit:LegendItem}" >
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type chartingToolkit:LegendItem}">
                                <Border >
                                    <StackPanel>
                                        <StackPanel Orientation="Horizontal" >
                                            <Rectangle Width="12" Height="12" Fill="{Binding ElementName=chart1,Path=Tag}"  Stroke="{Binding Background}" StrokeThickness="1"  />
                                            <datavis:Title Content="{TemplateBinding Content}" Foreground="{Binding ElementName=chart1,Path=Tag}" FontSize="18" Margin="10"/>
                                        </StackPanel>
                                    </StackPanel>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </chartingToolkit:LineSeries.LegendItemStyle>
        </chartingToolkit:LineSeries>
        <chartingToolkit:LineSeries Name="chart2" Tag="Green"   Style="{StaticResource LineSeriesStyle1}" Title="Preu KW"   DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding [1]}" IsSelectionEnabled="True" >
            <chartingToolkit:LineSeries.LegendItemStyle>
                <Style TargetType="{x:Type chartingToolkit:LegendItem}" >
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type chartingToolkit:LegendItem}">
                                <Border >
                                    <StackPanel>
                                        <StackPanel Orientation="Horizontal" >
                                            <Rectangle Width="12" Height="12" Fill="{Binding ElementName=chart2,Path=Tag}"   StrokeThickness="1"  />
                                            <datavis:Title Content="{TemplateBinding Content}" Foreground="{Binding ElementName=chart2,Path=Tag}" FontSize="18" Margin="10"/>
                                        </StackPanel>
                                    </StackPanel>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </chartingToolkit:LineSeries.LegendItemStyle>
        </chartingToolkit:LineSeries>
    </chartingToolkit:Chart>
</Grid>
InitializeComponent();

        List<KeyValuePair<DateTime, int>> llistaGastats = new List<KeyValuePair<DateTime, int>>();
        llistaGastats.Add(new KeyValuePair<DateTime, int>(DateTime.Now, 100));
        llistaGastats.Add(new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(1), 200));
        List<KeyValuePair<DateTime, int>> llistaPreu = new List<KeyValuePair<DateTime, int>>();
        llistaPreu.Add(new KeyValuePair<DateTime, int>(DateTime.Now, 300));
        llistaPreu.Add(new KeyValuePair<DateTime, int>(DateTime.Now.AddMonths(1), 300));
        var dataSourceList = new List<List<KeyValuePair<DateTime, int>>>();
        dataSourceList.Add(llistaGastats);
        dataSourceList.Add(llistaPreu);
        lineChart.DataContext = dataSourceList;
InitializeComponent();
List LISTAGASTATS=新列表();
添加(新的KeyValuePair(DateTime.Now,100));
添加(新的KeyValuePair(DateTime.Now.AddMonths(1200));
List listtapreu=新列表();
添加(新的KeyValuePair(DateTime.Now,300));
添加(新的KeyValuePair(DateTime.Now.AddMonths(1300));
var dataSourceList=新列表();
dataSourceList.Add(listagastats);
dataSourceList.Add(llistaPreu);
lineChart.DataContext=dataSourceList;
结果


你好,希娜。谢谢你的回复。我如何通过代码实现这一点,因为我必须动态添加几个系列。我看到您在xaml中添加了两个系列,并使用两个键值对项更新了datacontext。在我的例子中,lineseries的数量以前未知,但在运行时确定。如果我向datacontext Heena中添加两个以上的项,代码会工作吗?感谢您的快速回复和帮助:)