Search code examples
c#wpfwpf-controlswpfdatagrid

Passing parameters to a Page using constructors


In my application I have two pages placed on a Navigation Window.

Page 1 the Main Page and has three buttons on top ( like a ribbon menu) and a Frame ( on second half of the page to navigate within frame)

Page 2 - is the second page that will be directed within the frame when the buttons in Page 1 is clicked.

In Page 2 I have a Datagrid displaying list of servers for test environment - TestServer1 .. TestServerN, for development -DevServer1...DevServern and for Production -PrdServer1..PrdServerN.

currently when these button's are cliked , all the servers get displayed in the datagrid, but my concern is to display in the following order,

when user clicks

tstbutton ( test) - it should navigate to page 2 and display only servers which belongs to test system.

devbutton ( dev)- it should navigate to page 2 and display only servers which belongs to development system.

prdbutton (prd) - it should navigate to page 2 and display only servers which belongs to production system.

I have tried the following, but I could achieve only for one server, by passing paramter through constructor.

Page1.cs

public partial class Page1 : Page
{
public Page1()
{
 InitializeComponent();
}

private void PRD_Btn_Click(object sender, RoutedEventArgs e)
{
//  Server x = new Server();
Page2 pg = new Page2();
this.NavigationService.Navigate(pg);
}

private void TST_Btn_Click(object sender, RoutedEventArgs e)
{
// this works, only if I pass one paramaterized constructor
Server s = new Server();
Page2 pg = new Page2(s);
this.NavigationService.Navigate(pg);
}

private void DEV_Btn_Click(object sender, RoutedEventArgs e)
{
//  Server y = new Server();
Page2 pg = new Page2();
this.NavigationService.Navigate(pg);
}
}

Page2.cs

public partial class Page2: Page
{       
public Page2()
{
InitializeComponent();
}   
public Page2(Server s)
{
Db_Entities db = new Db_Entities();
string tst = "TEST";
var query1 = (from a in this.db.Servers
             where a.ServerID.Contains(tst)
             orderby a.ServerID
             select a).ToList();
datagrid1.ItemsSource = query1.ToList();
} 
}

The above code works fine.. but if I uncomment the code for the other two click event's for the prdtst and devtst button, and I pass tha parameters along with (Server s) .. there is ambiguity and errors.. how to achieve for the other two servers ? any ideas ??

EDIT

I have following are the errors when I add these extra blocks to my code

public Page2(Server x)
{
Db_Entities db = new Db_Entities();
string prd = "PRD";
var query1 = (from a in this.db.Servers
             where a.ServerID.Contains(prd)
             orderby a.ServerID
             select a).ToList();
datagrid1.ItemsSource = query1.ToList();
} 
public Page2(Server x)
{
Db_Entities db = new Db_Entities();
string dev = "DEV";
var query1 = (from a in this.db.Servers
             where a.ServerID.Contains(dev)
             orderby a.ServerID
             select a).ToList();
datagrid1.ItemsSource = query1.ToList();
} 

Error 1 - I get this error 2 times Type 'FN_UI.Views.Page2' already defines a member called 'Page2' with the same parameter types

Error 2 - I get this error 3 times The call is ambiguous between the following methods or properties: 'FN_UI.Views.Page2.Page2(FN_UI.Server)' and 'FN_UI.Views.Page2.Page2(FN_UI.Server)'

link for the complete code - https://gist.github.com/userXemY/c477c25c0c1641470c35


Solution

  • You cannot have three constructors with the same parameter type(s) in one class.

    And do you actually use the Server s constructor parameter anywhere?

    Anyway, write only one constructor by adding the search string as another parameter:

    public Page2(Server s, string what)
    {
        InitializeComponent(); // and don't forget this
    
        Db_Entities db = new Db_Entities();
        var query1 = (from a in this.db.Servers
                      where a.ServerID.Contains(what)
                      orderby a.ServerID
                      select a).ToList();
        datagrid1.ItemsSource = query1.ToList();
    
        // where is s used?
    } 
    

    and then call it like this:

    Server s = new Server();
    Page2 pg = new Page2(s, "TEST");
    

    and

    Server s = new Server();
    Page2 pg = new Page2(s, "PRN");
    

    and

    Server s = new Server();
    Page2 pg = new Page2(s, "DEV");