Silverlight ComboBox附加行为

时间:2010-03-15 10:59:13

标签: silverlight silverlight-3.0 attached-properties attachedbehaviors

我正在尝试创建一个可以应用于Silverlight ComboBox的附加行为。

我的行为是:

using System.Windows.Controls;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace AttachedBehaviours
{
    public class ConfirmChangeBehaviour 
    {

        public static bool GetConfirmChange(Selector cmb)
        {
            return (bool)cmb.GetValue(ConfirmChangeProperty);
        }

        public static void SetConfirmChange(Selector cmb, bool value)
        {
            cmb.SetValue(ConfirmChangeProperty, value);
        }


        public static readonly DependencyProperty ConfirmChangeProperty =
            DependencyProperty.RegisterAttached("ConfirmChange", typeof(bool), typeof(Selector), new PropertyMetadata(true, ConfirmChangeChanged));
        public static void ConfirmChangeChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            Selector instance = d as Selector;

            if (args.NewValue is bool == false)
                return;

            if ((bool)args.NewValue)
                instance.SelectionChanged += OnSelectorSelectionChanged;
            else
                instance.SelectionChanged -= OnSelectorSelectionChanged;

        }

        static void OnSelectorSelectionChanged(object sender, RoutedEventArgs e)
        {
            Selector item = e.OriginalSource as Selector;

            MessageBox.Show("Unsaved changes. Are you sure you want to change teams?");    

        }

    }

}

这在XAML中用作:

<UserControl x:Class="AttachedBehaviours.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:this="clr-namespace:AttachedBehaviours"
             mc:Ignorable="d">
    <Grid x:Name="LayoutRoot">
        <StackPanel>
            <ComboBox ItemsSource="{Binding Teams}" 
                      this:ConfirmChangeBehaviour.ConfirmChange="true" >
            </ComboBox>
        </StackPanel>
    </Grid>
</UserControl>

我收到错误:

  

元素ComboBox上的未知属性ConfirmChangeBehaviour.ConfirmChange。 [行:13位置:65]

Intellisense正在接受行为,为什么这会在运行时失败?

谢谢, 标记

编辑:注册()更改为RegisterAttached()。出现相同的错误。

2 个答案:

答案 0 :(得分:4)

您错误注册了附属财产

public static readonly DependencyProperty ConfirmChangeProperty =
        DependencyProperty.RegisterAttached("ConfirmChange", typeof(bool), typeof(Selector), new PropertyMetadata(true, ConfirmChangeChanged));

应该是

public static readonly DependencyProperty ConfirmChangeProperty =
        DependencyProperty.RegisterAttached("ConfirmChange", typeof(bool), typeof(ConfirmChangeBehaviour), new PropertyMetadata(true, ConfirmChangeChanged));

我建议你转而使用Blend Interactivity Behaviors。编写XAML而不是使用工具永远不会让设计师感到高兴。

答案 1 :(得分:1)

你需要改变这个:

DependencyProperty.Register("ConfirmChange"...

到此:

DependencyProperty.RegisterAttached("ConfirmChange"...

必须使用RegisterAttached而不是普通的Register。

来注册附加属性(包括附加行为)
相关问题