是否可以在自定义控件中存储对页面的引用,以便在单击控件时加载该页面(如自定义菜单项)?
到目前为止我的代码:
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)));
}
}
代码编译好了,但是如何将页面(比如类称为vpn)分配给XAML中的Page属性?
答案 0 :(得分:0)
如果您的“应用”使用NavigationWindow
代替Window
,那么您可以转到NavigationService
并告诉它更改页面。
protected override void OnClick()
{
NavigationService ns = NavigationService.GetNavigationService(this);
ns.Navigate( Page );
}