0

I'm aware that the related questions are already available in this site. I tried them but none of them are not providing the exact answer to my question. I am building a WPF application and I have a C# class which calls an XAML page to be loaded. here is my source page :

 public class ChartController : INotifyPropertyChanged
{
    public ObservableCollection<string> ChartTypes { get; set; }

    public ChartController()
    {
        ChartTypes = new ObservableCollection<string>();
        ChartTypes.Add("Pie");
        ChartTypes.Add("Doughnut");
        ChartTypes.Add("Clustered Bar");
        ChartTypes.Add("Clustered Column");
        ChartTypes.Add("Stacked Bar");
        ChartTypes.Add("Stacked Column");
        ChartTypes.Add("Stacked Bar Percentage");
        ChartTypes.Add("Stacked Column Percentage");
    }

    private string _simpleStringProperty;
    public string SimpleStringProperty
    {
        get { return _simpleStringProperty; }
        set
        {
            _simpleStringProperty = value;
            if (value.Equals("Pie"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\PieChart.xaml?parameter=test", UriKind.Relative);
            }
            if (value.Equals("Doughnut"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\DoughnutChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Clustered Column"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\ClusteredColumnChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Clustered Bar"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\ClusteredBarChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Bar"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedBarChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Column"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedColumnChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Bar Percentage"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedBarChart100Percent.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Column Percentage"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedColumnChart100Percent.xaml", UriKind.Relative);
            }
            if (value.Equals("Radial Gauge"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\RadialGaugeChart.xaml", UriKind.Relative);
            }
            OnPropertyChanged("SimpleStringProperty");

        }
    }

    private Uri _selectedPageChart;
    public Uri SelectedPageChart
    {
        get { return _selectedPageChart; }
        set
        {
            _selectedPageChart = value;
            OnPropertyChanged("SelectedPageChart");
        }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

My question is how can I catch the passed parameter ("test") in the PieChart.xaml.cs class and assign it to a variable ?

here is my piechart.xaml

 <Page x:Class="ModernUIForWPFSample.WithoutBackButton.Graphs.GraphTemplates.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:metroChart="clr-namespace:De.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart" 
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">

<Grid>

    <metroChart:PieChart x:Name="pieChart"
Style="{StaticResource MinimalChartStyle}"
ChartTitle="Minimal Pie Chart"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" >
        <metroChart:PieChart.Series>
            <metroChart:ChartSeries x:Name="pieChartSeries"
        SeriesTitle="Errors"
        DisplayMember="Year"
        ValueMember="Cost"
        ItemsSource="{Binding Path=Errors}" />
        </metroChart:PieChart.Series>
    </metroChart:PieChart>

</Grid>

I'm using Visual Studio 2013 DotNet framework 4.5 and WPF

1 Answers1

0

You can extract parameters via NavigationService.CurrentSource which returns a Uri object. But the better approach would be create instance of specific page and use the overload for NavigationService.Navigate method, which takes an object for the parameter.

private Page _selectedPage;
private string _simpleString;

public string SimpleString
{
   get { /* ... */ }
   set 
   {
        _simpleStringProperty = value;
        if (value.Equals("Pie"))
        {
            SelectedPage = new PieChart("test");
        }
        /* rest of the setter */
   }
}
Lukas Kubis
  • 929
  • 5
  • 17