wpf xaml 资源字典添加自定义属性 [英] wpf xaml resource dictionary add custom property

查看:69
本文介绍了wpf xaml 资源字典添加自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些页面中使用以下 xaml 代码进行导航:

I want to use the following xaml code for navigation in some pages:

<Button Content="Go to page2">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <ei:ChangePropertyAction PropertyName="Source" TargetObject="{Binding NavigationService, RelativeSource={RelativeSource AncestorType={x:Type Page}, Mode=FindAncestor}}">
                <ei:ChangePropertyAction.Value>
                    <System:Uri>Page2.xaml</System:Uri>
                </ei:ChangePropertyAction.Value>
            </ei:ChangePropertyAction>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

所以我想知道是否有可能将交互部分外包到样式中(在资源字典中)并添加一个像NavigationUri"这样的自定义属性,您可以在其中直接声明要导航到的页面.
另一个想法(这可能是更好的方法)是创建一个自定义控件并从按钮类继承.在任何地方,我都更喜欢更紧凑、更精简的方式,而无需背后代码.

So I´m wondering if there is a possibility to outsource the interaction part into a style (in a resource dictionary) and add a custom property like "NavigationUri" where you can directly declare the page to navigate to.
Another idea (which would probably be the better approach) is to create a custom control and inherit from button class. Anywhere I would prefer a more compact and lean way without code behind.

请告诉我,哪个是更合适的解决方案以及如何实施它.

Please let me know, which is the more suitable solution and how to implement it.

推荐答案

尽管有各种简单的技术可以使我们的 Behavior Xaml 成为静态资源.但是,我们需要自定义行为,因为我们使用页面名称形式的参数进行导航.这个变量需要编程.

Although there are various simple techniques to make our Behavior Xaml as static resource. But, we need a custom behavior, as we are using a parameter in the form of Page name to navigate to. This variable demands programming.

所以,我想出了

一个.自定义行为(NavigationBehavior),以及

a. Custom behavior(NavigationBehavior), and

B.按钮子类化(NavigationButton)

b. Button subclassing(NavigationButton)

导航行为

using System;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace WpfApplication1.Navigation
{
    public class NavigationBehavior:Behavior<NavigationButton>   
    {
        protected override void OnAttached()
        {
            AssociatedObject.Click += AssociatedObject_Click;
            base.OnAttached();
        }

        void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ((Page)AssociatedObject.DataContext).NavigationService.Source = new Uri(AssociatedObject.DestinationUri, UriKind.Relative);
        }
    }
}

导航按钮

namespace WpfApplication1.Navigation
{
    public class NavigationButton : Button
    {
        NavigationBehavior behavior = new NavigationBehavior();

        public NavigationButton()
        {            
            behavior.Attach(this);
        }

        public string DestinationUri { get; set; }
    }
}

用法:

<nav:NavigationButton Content="Navigate to Page2" DestinationUri="/Navigation/Page2.xaml" />

重要提示

  1. 我们在行为中使用 DataContext 属性来访问包含页面.因此,在所有页面的构造函数中设置 this.DataContext = this; .

可以尝试使用通用基类/接口来避免这种情况.

One can try using a common base class / interface to avoid this.

这篇关于wpf xaml 资源字典添加自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆