user2376998
user2376998

Reputation: 1071

c# WPF button link to user control page

I have a MainWindow which only have a button , How do i link this button to a user control page ( home.xaml) ? i am new to WPF in my mainwindow :

<Button Height="100" Name="btnHome" Width="103" Click="btnHome_Click"/>

in my mainwindow.cs:

    private void btnHome_Click(object sender, RoutedEventArgs e)
    {
        var home = new home();
        home.Show();
    }

but it doesnt work . home.xaml is my user control page that i want to link to by clicking on the button in mainwindow.

Upvotes: 2

Views: 3955

Answers (4)

Julio F
Julio F

Reputation: 1

Window w = new Window();
w.Content = new UserControl1();
w.Show();

Upvotes: 0

Akhil Fernandez
Akhil Fernandez

Reputation: 1

Use this code on button and set your usercontrol name or path in uri.

private void btn_Click(object sender, RoutedEventArgs e)
{
    NavigationService.GetNavigationService(this)
       .Navigate(new Uri("/UserControls/GameDetails.xaml", UriKind.RelativeOrAbsolute));
}

Upvotes: 0

sa_ddam213
sa_ddam213

Reputation: 43606

You should be able to use the NavigationService to navigate between WPF Pages

Example:

private void btnHome_Click(object sender, RoutedEventArgs e)
{
    NavigationService.GetNavigationService(this).Navigate("home.xaml");
}

Upvotes: 2

user1715416
user1715416

Reputation:

 <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <br />
 <asp:Button ID="Button1" runat="server" Text="Next" onclick="Button1_Click" />
Page1.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["Name"] != null)
                txtName.Text = Session["Name"].ToString();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["Name"] = txtName.Text;
        Response.Redirect("Page2.aspx");
    }
Page2.aspx

<asp:Button ID="Button1" runat="server" Text="Back" onclick="Button1_Click" />
Page2.aspx.cs

 protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Page1.aspx");
    }

Upvotes: 0

Related Questions