简体   繁体   中英

WPF Custom Control Property - Assign object such as page

Is it possible to store a reference to a Page in a Custom Control so that when the control is clicked that page loads (like a custom menu item)?

My code so far:

public class ccMenuItem : Button
{
     public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(Control));
     public static readonly DependencyProperty BackColorProperty = DependencyProperty.Register("BackColor", typeof(SolidColorBrush), typeof(Control), new UIPropertyMetadata(Brushes.White));
     public static readonly DependencyProperty PageProperty = DependencyProperty.Register("Page", typeof(Page), typeof(Control));


     public Page Page
     {
         get { return (Page)GetValue(PageProperty); }
         set { SetValue(PageProperty, value); }
     }

    public string Title
    {
        get { return GetValue(TitleProperty).ToString(); }
        set { SetValue(TitleProperty, value); }
    }

    static ccMenuItem()
    {

        DefaultStyleKeyProperty.OverrideMetadata(typeof(ccMenuItem), new FrameworkPropertyMetadata(typeof(ccMenuItem)));
    }
}

The code compiles OK, however how do I assing a page (say the class is called vpn) to the Page property in XAML?

If your "application" is using a NavigationWindow instead of Window , then you can get to the NavigationService and tell it to change the page.

protected override void OnClick()
{
    NavigationService ns = NavigationService.GetNavigationService(this);

    ns.Navigate( Page );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM