使用xaml样式创建控件,在wpf中使用for循环

时间:2018-05-18 09:42:37

标签: c# wpf xaml styles

您好我想使用for循环创建许多图表,所以这是我的简单图表代码:

<Grid Margin="15, 20, 15, 15" Width="280" Height="310">
            <Grid.Effect>
                <DropShadowEffect BlurRadius="15" Direction="-90" RenderingBias="Quality" Opacity=".2" ShadowDepth="1"/>
            </Grid.Effect>
            <Grid.OpacityMask>
                <VisualBrush Visual="{Binding ElementName=Border2}" />
            </Grid.OpacityMask>
            <Grid.Resources>
                <Style TargetType="lvc:ColumnSeries">
                    <Setter Property="StrokeThickness" Value="0"></Setter>
                    <Setter Property="Stroke" Value="White"></Setter>
                    <Setter Property="Fill" Value="White"></Setter>
                    <Setter Property="MaxColumnWidth" Value="5"></Setter>
                </Style>
                <Style TargetType="lvc:Axis">
                    <Setter Property="FontSize" Value="12"></Setter>
                    <Setter Property="Foreground" Value="#64FFFFFF"></Setter>
                    <Style.Triggers>
                        <Trigger Property="AxisOrientation" Value="Y">
                            <Setter Property="IsMerged" Value="True"></Setter>
                            <Setter Property="MaxValue" Value="10"></Setter>
                        </Trigger>
                    </Style.Triggers>
                </Style>

            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height=".50*"></RowDefinition>
                <RowDefinition Height=".5*"></RowDefinition>
            </Grid.RowDefinitions>
            <Border x:Name="Border2" Grid.Row="0" Grid.RowSpan="4" CornerRadius="5" Background="White" />
            <Border Grid.Row="0" Grid.RowSpan="3" Background="#EB5A13" ></Border>
            <TextBlock Grid.Row="0" TextAlignment="Center" Padding="10, 10, 0, 5" Foreground="White" FontSize="18">
                    Time Power
            </TextBlock>
            <TextBlock Grid.Row="1" TextAlignment="Center" Foreground="#59FFFFFF" Padding="0,0,0,20">2014.12.25</TextBlock>
            <lvc:CartesianChart Name="TimePowerChart" Grid.Row="2" Margin="10, 0, 10, 20"  Hoverable="False" DataTooltip="{x:Null}">
                <lvc:CartesianChart.Series>
                    <lvc:ColumnSeries/>
                </lvc:CartesianChart.Series>
            </lvc:CartesianChart>
        </Grid>

我可以使用以下代码创建许多图表:

CartesianChart[] mychart = new CartesianChart[4];
            for (int i = 0; i < mychart.Length; i++)
            {
                var chrt = new CartesianChart();
                mychart[i] = chrt;
                myStack.Children.Add(chrt);

            } 

但是使用这些代码,只创建图表并且不应用样式,如何将所有样式应用于创建的图表?

0 个答案:

没有答案