Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change frame source from other windows in wpf

Tags:

c#

wpf

I have 2 window in wpf application. There is a frame in window1. I want to change frame source from window2. can you help me?

for example: window 1:

<frame x:name="frame1"/>

window2.cs :

private void button1_click(object sender, RoutedEventArgs e){
window1.frame1.source = new Uri("page1.xaml",UriKind.Relative);
}
like image 892
amirhossein Avatar asked Oct 20 '25 07:10

amirhossein


1 Answers

Set the FieldModifier attribute of the Frame to internal or public or expose the Frame through a property in Window1:

<Frame x:Name="frame1" x:FieldModifier="public" />

You can then get a reference to Window1 and access the field or property using the Application.Current.Windows collection:

private void button1_click(object sender, RoutedEventArgs e)
{
    Window1 window1 = Application.Current.Windows.OfType<Window1>().FirstOrDefault();
    if (window1 != null)
    {
        window1.frame1.Source = new Uri("page1.xaml", UriKind.Relative);
    }
}
like image 51
mm8 Avatar answered Oct 22 '25 23:10

mm8