代码之家  ›  专栏  ›  技术社区  ›  Mark Cooper

Silverlight ComboBox附加行为

  •  3
  • Mark Cooper  · 技术社区  · 15 年前

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

    我的行为是:

    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>
    

    我得到一个错误:

    元素组合框上的未知属性confirmChangeBehavior.ConfirmChange[行:13位置:65]

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

    作记号

    Register()已更改为registeratached()。出现同样的错误。

    2 回复  |  直到 15 年前
        1
  •  4
  •   Graeme Bradbury    15 年前

    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));
    

    我建议你继续使用混合互动行为。编写XAML而不是使用工具永远不会让设计者高兴。

        2
  •  1
  •   itowlson    15 年前

    DependencyProperty.Register("ConfirmChange"...
    

    对此:

    DependencyProperty.RegisterAttached("ConfirmChange"...
    

    附加属性(包括附加行为)必须使用RegisterAttached而不是普通的旧寄存器进行注册。

    推荐文章