Onur İn
Onur İn

Reputation: 39

WPF Using of Frame

While I'm using frame in Mainwindow , initially i hide an item in Mainwindows. When i pressed a button in frame Page1 , I want to make item in mainwindow as visible.But i can't do it.I tried to updatelayout() , refresh() functions but anything is changed.Anyone has a knowledge about this??

This code is in MainWindow

 private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                müsteributton.IsEnabled = false;
                string yer = "Pages/kullanicigiris.xaml";
                frame1.Source = new Uri(yer, UriKind.Relative);
                frame1.Margin = new Thickness(-175, 0, 0, 0);

            }

This code is in kullanicigiris page

 private void Dispatcher_Tick(object sender, EventArgs e)
        {
            i++;
            if (i == 2)
            {

                dispatcher.Stop();
                frm1 = new MainWindow();
                 frm1.frame1 = null;
                DependencyObject currParent = VisualTreeHelper.GetParent(this);
                while (currParent != null && frm1.frame1 == null)
                {
                    frm1.frame1 = currParent as Frame;
                    currParent = VisualTreeHelper.GetParent(currParent);
                }

                // Change the page of the frame.
                if (frm1.frame1 != null)
                {
                    frm1.frame1.Source = new Uri("Window1.xaml", UriKind.Relative);
                    frm1.müsteributton.IsEnabled = true;
                }


            }

        }

Thanks.

Upvotes: 1

Views: 186

Answers (2)

Peter Duniho
Peter Duniho

Reputation: 70652

Your bug is here:

frm1 = new MainWindow();

You are creating a brand new window, and then making your changes in that window.

But: that's not the window the user's looking at!

Taking the approach you've embarked on, your frame code needs to keep track of the Window object it's actually being hosted in, and then use that reference for dealing with the update.

That said, that entire approach is flawed. The navigation should be modeled in a view model data structure, activated via an ICommand object, and optionally via timer (as you seem to be doing here). Frame source and button state can be manipulated through bindings to properties in your view model data structure.

But, at the end of the day, the code you've got should work fine, once you start using the correct Window object.

Upvotes: 1

Yohann
Yohann

Reputation: 234

You can define a DependencyProperty in the MainWindows.

  <TextBlock x:Name="textBlock" Height="399" TextWrapping="Wrap" Text="Show/ Hide" VerticalAlignment="Top" Visibility="{Binding SetVisibility, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

  public static readonly DependencyProperty SetVisibilityProperty =
     DependencyProperty.Register("SetVisibility", typeof(Visibility), typeof(Mainfreampage), new
        PropertyMetadata(Visibility.Visible, null));

    public Visibility SetVisibility
    {
        get { return (Visibility)GetValue(SetVisibilityProperty); }
        set { SetValue(SetVisibilityProperty, value); } 
    }

In your page click event, you can use the following code find the MainWindows and change the DependencyProperty value.

  var mw = Application.Current.Windows
.Cast<Mainfreampage>()
.FirstOrDefault(window => window is Mainfreampage) as Mainfreampage;


  mw.SetVisibility = Visibility.Hidden;

Upvotes: 2

Related Questions