c# - 用C#动态改变元素的样式

标签 c# silverlight windows-phone-7

我有一个包含 4x LineSeries 的图表。我定义了两种不同的样式来表示线条,我希望能够动态更改应用于特定 LineSeries 的样式(例如,基于用户点击按钮等)。

我似乎不知道如何从 C# 更新样式。任何帮助表示赞赏!

我试过:

lineChartMood.PolylineStyle = this.Resources.PolylineStyle2 as Style;

但这会返回 Null 异常。

这是页面的 XAML,包括样式定义:

    <phone:PhoneApplicationPage 
    x:Class="Bhutaan.ChartingTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:local="clr-namespace:Bhutaan"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <Style x:Key="PolylineStyle" TargetType="Polyline">
            <Setter Property="StrokeThickness" Value="5"/>
        </Style>
        <Style x:Key="PolylineStyle2" TargetType="Polyline">
            <Setter Property="StrokeThickness" Value="1"/>
        </Style>
    </phone:PhoneApplicationPage.Resources>


    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="TEST" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="added" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,-0,12,0">
            <ScrollViewer>
                <StackPanel>
                    <TextBlock Text="Great! That's been saved." FontSize="30" Margin="0,0,0,0"/>
                    <!-- Chart  -->
                    <charting:Chart
                        x:Name="myChart"
                        Margin="0,20,0,0"
                        Height="350"
                        Style="{StaticResource PhoneChartStyle}"
                        Template="{StaticResource PhoneChartPortraitTemplate}">

                        <!-- Series -->
                        <charting:LineSeries
                            x:Name="lineChartMood"
                            Title="Mood"
                            ItemsSource="{Binding}"
                            DependentValuePath="MoodValue"
                            IndependentValuePath="Timestamp"
                            PolylineStyle="{StaticResource PolylineStyle}" >

                            <charting:LineSeries.LegendItemStyle>
                                <Style TargetType="charting:LegendItem">
                                    <Setter Property="Margin" Value="5 0 5 0"/>
                                </Style>
                            </charting:LineSeries.LegendItemStyle>
                        </charting:LineSeries>
                        <!-- Series -->
                        <charting:LineSeries
                            Title="Energy"
                            ItemsSource="{Binding}"
                            DependentValuePath="EnergyValue"
                            IndependentValuePath="Timestamp">
                            <charting:LineSeries.LegendItemStyle>
                                <Style TargetType="charting:LegendItem">
                                    <Setter Property="Margin" Value="5 0 5 0"/>
                                </Style>
                            </charting:LineSeries.LegendItemStyle>
                        </charting:LineSeries>
                        <!-- Series -->
                        <charting:LineSeries
                            Title="Mental"
                            ItemsSource="{Binding}"
                            DependentValuePath="MentalValue"
                            IndependentValuePath="Timestamp">
                            <charting:LineSeries.LegendItemStyle>
                                <Style TargetType="charting:LegendItem">
                                    <Setter Property="Margin" Value="5 0 5 0"/>
                                </Style>
                            </charting:LineSeries.LegendItemStyle>
                        </charting:LineSeries>
                        <!-- Series -->
                        <charting:LineSeries
                            Title="Hunger"
                            ItemsSource="{Binding}"
                            DependentValuePath="HungerValue"
                            IndependentValuePath="Timestamp">
                            <charting:LineSeries.LegendItemStyle>
                                <Style TargetType="charting:LegendItem">
                                    <Setter Property="Margin" Value="5 0 5 0"/>
                                </Style>
                            </charting:LineSeries.LegendItemStyle>
                        </charting:LineSeries>
                    </charting:Chart>

                </StackPanel>
            </ScrollViewer>
        </Grid>
    </Grid>


</phone:PhoneApplicationPage>

最佳答案

我测试了你的代码,找到了问题的原因。

您收到 NullReferenceException 是因为应用程序出于某些未知原因未设置字段 this.lineChartMood

您必须自己获取此线系列对象。有两种可能的获取方式:

var lineSeriesWay1 = this.FindName("lineChartMood");
var lineSeriesWay2 = myChart.Series.OfType<LineSeries>().First(ls => ls.Name == "lineChartMood");

但我建议在构造函数中显式设置字段 this.lineSeriesMood,如下所示:

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        // because the code 'this.FindName("lineChartMood")' doesn't work in the constructor, I'll use the following line:
        this.lineChartMood = this.myChart.Series.OfType<LineSeries>().First(ls => ls.Name == "lineChartMood");

        // other code
    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.lineChartMood.PolylineStyle = (Style)this.Resources["PolylineStyle2"];
        this.lineChartMood.Refresh(); // you should call this method so that the style is applied
    }
}

然后您将能够毫无异常(exception)地引用您的系列。

关于c# - 用C#动态改变元素的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12128556/

相关文章:

c# - 如何覆盖 WPF 上的 onclose 事件?

silverlight - 具有 RIA 服务的 Entity Framework ,Silverlight - 解耦与快速开发的权衡

windows-phone-7 - Windows Phone 7 的增强现实浏览器?

silverlight - Windows Phone 7 用户界面是否基于 Silverlight?

silverlight - NavigationService.Navigate() 方法和 PhoneApplicationFrame.Source 属性有什么区别?

c# - 在 C# 中的服务器上使用 StreamWriter

c# - 如何在一行中传递元组结果

c# - 使用 Mono 针对 .NET Framework 4.0 进行定位和编译

silverlight - 如何从 silverlight 检查 javascript 函数是否存在

wpf - Visual Studio 2010 中的 prism 项目链接器发生了什么变化?