C# WPF自定义控件属性-分配对象,如页面
是否可以在自定义控件中存储对页面的引用,以便在单击该控件时加载页面(如自定义菜单项) 到目前为止,我的代码是:C# WPF自定义控件属性-分配对象,如页面,c#,wpf,C#,Wpf,是否可以在自定义控件中存储对页面的引用,以便在单击该控件时加载页面(如自定义菜单项) 到目前为止,我的代码是: public class ccMenuItem : Button { public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(Control)); public static readon
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)));
}
}
代码编译正常,但是如何在XAML中将页面(比如称为vpn的类)分配到页面属性?如果您的“应用程序”使用的是导航窗口而不是窗口
,那么您可以访问导航服务
,并告诉它更改页面
protected override void OnClick()
{
NavigationService ns = NavigationService.GetNavigationService(this);
ns.Navigate( Page );
}