2012-08-26 80 views
1

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

我似乎无法解决如何从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> 
+0

我想你会在右上角的分配线上得到你的nullpointerexception?哪个对象为null?它可能是lineChartMood或者“PolylineStyle2”写错了吗?你能告诉更多哪个对象是空的吗?另外,C#代码位于XAML后面的代码之上吗? –

+0

进一步的调试显示样式元素不为空......它是lineChartMood对象为null。为什么会这样?在代码编辑器中,它不会出现错误...但在运行时似乎为空... –

+0

准确而言:根据以下答案的反馈,我将该行更新为: lineChartMood.PolylineStyle = App.Current.Resources [“PolylineStyle2”]作为样式; –

回答

5

我测试了你的代码,发现问题的原因。

由于某些未知原因,应用程序未设置字段this.lineChartMood,您将得到NullReferenceException。

你必须自己获得这一系列的对象。有两种可能的方式来得到它:

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 
    } 
} 

然后你就可以参考你的系列无一例外。

+0

@vortex在构造函数好的体操:p(+1投票) –

+0

非常感谢这 - 工作!我现在可以无例外地运行代码。完善! (我现在有一个后续的问题...由于某些原因,我只能减少polyLineThickness属性的值...增加它不起作用。换句话说,如果它设置为5,我可以将其更改为1在运行时,但相反不起作用)。 再次感谢您的回答 - 抱歉花了这么长时间才接受并回复(过去几周我一直在上班)。 干杯! –

2

lineChartMood.PolylineStyle = this.Resources.PolylineStyle2 as Style; 应该 lineChartMood.PolylineStyle = this.Resources["PolylineStyle2"] as Style;

+0

刚刚尝试过...我得到一个NullReferenceException ... –

+0

定义你的app.xaml中的样式,并通过“application.current.resources [”PolylineStyle2“]” “访问它”我认为你不是当你尝试获取资源时,在同一页中? – TheHe

+0

绝对尝试过...风格本身不会返回为null,lineChartMood对象是。由于某种原因,它在运行时为空...不知道为什么,但上面的Gros Lalo认为它可能是我使用的图表库的一个错误... –

3

为什么不在App.xaml中添加资源并使用App.Current.Resources将其引用?我这样说是因为我觉得程序可能无法引用在特定页面上定义的本地资源。我可能错了。这不仅仅是一种解决方法。

+0

我只是试过这个。我仍然得到相同的空引用异常...我必须在这里做一些愚蠢的错误! –