1

I have a static property, inside a static class, that i have bound to Source property of a frame. It is a OneWay binding. The binding is correctly working the first time but doesn't update the target when property changes. This is my xaml

<Frame x:Name="frmMain" Source="{Binding Source={x:Static currentPage:ActivePages.MainFramePage}, NotifyOnTargetUpdated=True,Mode=OneWay}"/>

This is my static class ActivePages.cs


public static class ActivePages
    {
        private static Uri mainFramePage;
        public static Uri MainFramePage
        {
            get { return mainFramePage; }
            set
            {
                mainFramePage = value;
                MainFramePageChanged?.Invoke(null, new PropertyChangedEventArgs("MainFramePage"));
            }
        }
public static event EventHandler<PropertyChangedEventArgs> MainFramePageChanged;
}

I followed this link and another link
How do i update it, when source changes??
Community
  • 1
  • 1
Mavil
  • 282
  • 1
  • 2
  • 9

2 Answers2

0

You can use Path property if it is WPF 4.5 or higher:

 <Frame x:Name="frmMain" Source="{Binding Path=(local:ActivePages.MainFramePage), NotifyOnTargetUpdated=True,Mode=OneWay}"/>
rmojab63
  • 3,513
  • 1
  • 15
  • 28
0

According to the MSDN page the signature of a static property changed event is either

public static event EventHandler MyPropertyChanged;

or

public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

So yours should be

public static event EventHandler MainFramePageChanged;

instead of

public static event EventHandler<PropertyChangedEventArgs> MainFramePageChanged;

However, if you have multiple static properties, you should better use a single event

public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

with PropertyChangedEventArgs that provide property name information.


You should also use this syntax for the Binding expression:

Source="{Binding Path=(currentPage:ActivePages.MainFramePage)}"

See also this blog post for details about static property change notification.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • i am not able to access the link for the blog. and your syntax for specifying the source doesn't load the frame source – Mavil Feb 08 '17 at 10:46
  • Ok, the link seems to be broken, maybe just temporarily. The binding expression syntax may not work with older WPF versions. What version are you using? You may also take a look at this answer: http://stackoverflow.com/a/41823852/1136211 – Clemens Feb 08 '17 at 11:08
  • And of course the binding path should be `currentPage:ActivePages.MainFramePage`, not `local:ActivePages.MainFramePage`. – Clemens Feb 08 '17 at 11:30
  • Can you let me know the contents of the blog, cuz the link is still broken – Mavil Feb 10 '17 at 06:16