介绍性行为问题 [英] Introductory Behavior Question

查看:78
本文介绍了介绍性行为问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当我在MVVM中编写wpf应用程序时,我当然终于想到了要按照MVVM模式注册UIElement事件的情况.  因此,我设法拼凑而成,您可以使用行为"来做到这一点, 特别是,您可以使用行为"(例如,出于教育目的)注册按钮单击(假设每个UIElement提供了所有这些事件,并且由于我不知道的原因,您不能像在UIStudio中那样绑定这些事件. -每个属性旁边的小方块-我希望我知道为什么不行吗?).  因此要注册该按钮,请点击我的按钮,我会看到以下XAML:

< Window x:Class =" WpfApplication1.Window1"
    xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local ="clr-namespace:WpfApplication1"
    标题="Window1".高度="300".宽度="300".
    <网格>
        < Button Content ="Hello"; local:ClickBehavior.RightClick =&; {Binding Foo}"/>
    </Grid>
</Window>

足够简单,甚至看起来很有意义.  并且我的静态ClickBehavior类如下所示:

公共静态类ClickBehavior
    {
公共静态DependencyProperty RightClickCommandProperty = DependencyProperty.RegisterAttached("RightClick",
                    typeof(ICommand),
                    typeof(ClickBehavior),
                    新的FrameworkPropertyMetadata(null,
newPropertyChangedCallback(ClickBehavior.RightClickChanged)));
 
        公共静态无效SetRightClick(DependencyObject target,ICommandvalue)
        {
            target.SetValue(ClickBehavior.RightClickCommandProperty,value);
        }
 
        公共静态ICommand GetRightClick(DependencyObject目标)
{
            返回(ICommand)target.GetValue(RightClickCommandProperty);
        }
 
 
私有静态无效RightClickChanged(DependencyObject target,DependencyPropertyChangedEventArgs e)
        {
            UIElement element = target作为UIElement;
            如果(元素!=空)
            {
                //如果我们要输入一个新命令,但还没有一个
                //钩住事件
                if((e.NewValue!= null)&&(e.OldValue == null))
                {
                    element.MouseRightButtonUp + = element_MouseRightButtonUp;
                }
                //如果我们正在清除命令并且它还不是null
                //取消挂接事件
                否则if((e.NewValue == null)&&(e.OldValue!= null))
                {
                    element.MouseRightButtonUp-= element_MouseRightButtonUp;
                }
            }
        }
 
        静态void element_MouseRightButtonUp(object sender,MouseButtonEventArgs e)
        {
            UIElement element =(UIElement)sender;
            ICommand命令=(ICommand)element.GetValue(ClickBehavior.RightClickCommandProperty);
            command.Execute(null);
        }
    }//ClickBehavior的结尾

我的问题是:

1)为什么我们使用RegisterAttached而不是Register?

2)如果我们定义了一个名为"Bla"的DependencyProperty,必须提供方法"SetBla"和"GetBla",对吗?

3)我们如何以及何时在ClickBehavior中实际注册按钮?  也就是说,我们在代码中没有任何地方调用任何带有参数DependencyObject的方法 目标?? Button xaml定义是否自动将自身作为目标传递给RightClickChanged(Dependency Object target,DependencyPropertyEventArgs e)?  幕后到底发生了什么?

我知道这有点,非常感谢


MarcinMR

解决方案

嗨Marcin.

单击是不幸的选择,因为您将绑定命令而不是处理单击. (实际上,我很少写新的行为.)

您可能会发现这很有趣:

http://social.technet.microsoft.com/wiki /contents/articles/30564.wpf-uneventful-mvvm.aspx

请注意,您始终可以将点击绑定到任何命令上.

您可以将按钮模板化为任何东西,然后将命令绑定到按钮上.

http://social.technet.microsoft.com/wiki/contents/articles/29866 .aspx

您问了3个问题-应该是一个线程,伙计.

> 1)为什么我们使用RegisterAttached而不是Register?

1)

这是一个附加的依赖项属性,您可以使用registerattached.如果您将按钮子类化并创建了MyButton,则可以使用register.实际上,您的依赖项属性不是按钮的一部分.它是附件.

> 2)如果我们定义了一个名为"Bla"的DependencyProperty,则我们必须提供方法"SetBla"和"GetBla",对吗?

您需要为附加的dp处理.

不是所有的依赖项属性.如果您在窗口或其他东西后面打开代码.键入propdp,然后按Tab.这为您提供了基本的依赖项属性.没有设置或获取.

 public int MyProperty
        {
            获取{return(int)GetValue(MyPropertyProperty); }
            设置{SetValue(MyPropertyProperty,value); }
        }

        //使用DependencyProperty作为MyProperty的后备存储.这样可以实现动画,样式,绑定等.
        公共静态只读DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty",typeof(int),typeof(ownerclass),新的PropertyMetadata(0)); 

> 3)我们如何以及何时在ClickBehavior中实际注册按钮?

它自己做.该行为是在实例化其拥有按钮时实例化的对象.这是richtclickchanged的触发事件,它在其按钮上附加了一个处理程序.

.

只需强调一下,这对于点击处理程序来说是错误的方法.

右键单击也是错误的方法,因为您具有可以绑定命令的鼠标手势.


So as I program my wpf application in MVVM, I have of course finally come upon the case where I would like to register for an UIElement's event following the MVVM pattern.  And so I have managed to piece together that you can do this using Behaviors, in particular you could register for (let's say for educational purposes) a button click using a Behavior (There is all these events that each UIElement provides, and for reasons unbeknownst to me you can't bind these in VisualStudio as you can UIElement Properties - the little square beside each property - I wish I knew why not?).  So to register for that Button Click on my Button I have the following XAML:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Content="Hello" local:ClickBehavior.RightClick="{Binding Foo}"/>
    </Grid>
</Window>

Simple enough, it even seems to make sense.  And my static ClickBehavior class looks as follows:

public static class ClickBehavior
    {
public static DependencyProperty RightClickCommandProperty = DependencyProperty.RegisterAttached("RightClick",
                    typeof(ICommand),
                    typeof(ClickBehavior),
                    new FrameworkPropertyMetadata(null, 
newPropertyChangedCallback(ClickBehavior.RightClickChanged)));
 
        public static void SetRightClick(DependencyObject target, ICommandvalue)
        {
            target.SetValue(ClickBehavior.RightClickCommandProperty, value);
        }
 
        public static ICommand GetRightClick(DependencyObject target) 
{
            return (ICommand)target.GetValue(RightClickCommandProperty);
        }
 
 
private static void RightClickChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            UIElement element = target as UIElement;
            if (element != null)
            {
                // If we're putting in a new command and there wasn't one already
                // hook the event
                if ((e.NewValue != null) && (e.OldValue == null))
                {
                    element.MouseRightButtonUp += element_MouseRightButtonUp;
                }
                // If we're clearing the command and it wasn't already null
                // unhook the event
                else if ((e.NewValue == null) && (e.OldValue != null))
                {
                    element.MouseRightButtonUp -= element_MouseRightButtonUp;
                }
            }
        }
 
        static void element_MouseRightButtonUp(object sender,MouseButtonEventArgs e)
        {
            UIElement element = (UIElement)sender;
            ICommand command = (ICommand)element.GetValue(ClickBehavior.RightClickCommandProperty);
            command.Execute(null);
        }
    }//End of ClickBehavior

The questions that I have are:

1)why do we use RegisterAttached and not Register?

2) If we define a DependencyProperty called "Bla", then we must provide the methods "SetBla" and "GetBla", correct?

3)How and when do we actually register our button with ClickBehavior?  That is, nowhere in the code do we ever call any of the methods that take the parameter DependencyObject target?? Does the Button xaml definition automatically call RightClickChanged(Dependency Object target, DependencyPropertyEventArgs e) passing itself as target?  What exactly happens behind the scenes?

I know it's a bit, so much Thanks


MarcinMR

解决方案

Hi Marcin.

Click was an unfortunate choice, because you would bind a command rather than handling click. ( In fact I fairly rarely write new behaviors.)

You might find this interesting:

http://social.technet.microsoft.com/wiki/contents/articles/30564.wpf-uneventful-mvvm.aspx

Note that you can always bind a click to a command on anything.

You can template a button as anything and then bind the command on the button.

http://social.technet.microsoft.com/wiki/contents/articles/29866.aspx

You asked 3 questions - it's supposed to be just one a thread, mate.

>1)why do we use RegisterAttached and not Register?

1)

It's an attached dependency property, you use registerattached. You could use register if you subclassed your button and created a MyButton. As it is, your dependency property is not part of the button. It's attached.

>2) If we define a DependencyProperty called "Bla", then we must provide the methods "SetBla" and "GetBla", correct?

You do for attached dp.

Not for all dependency properties. If you open code behind of a window or something.  Type propdp then tab. That gives you a basic dependency property. No set or get.

        public int MyProperty
        {
            get { return (int)GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(int), typeof(ownerclass), new PropertyMetadata(0));

>3)How and when do we actually register our button with ClickBehavior?

It does it itself. The behaviour is an object which is instantiated when it's owning button is instantiated. It's richtclickchanged fires and it attaches a handler to it's button.

.

Just to emphasise, this is the wrong approach for a click handler.

It's also the wrong approach for a right click because you have a mousegesture which you can bind the command of.


这篇关于介绍性行为问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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