代码之家  ›  专栏  ›  技术社区  ›  Konamiman

在自定义行为上绑定依赖属性时出错

  •  10
  • Konamiman  · 技术社区  · 16 年前

    InitializeComponent


    <UserControl x:Class="MyExample.MyPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyExample"
        >
        <Grid x:Name="LayoutRoot">
            <local:MyView/>
        </Grid>
    </UserControl>
    


    <UserControl x:Class="MyExample.MyView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:local="clr-namespace:MyExample"
        Width="400" Height="300">
        <StackPanel Orientation="Vertical" x:Name="LayoutRoot" Background="White">
            <StackPanel.Resources>
                <local:MyViewmodel x:Key="MyResource"/>
            </StackPanel.Resources>
            <TextBlock Text="This button will display the following message:"/>
            <TextBlock Text="{Binding MyMessage, Source={StaticResource MyResource}}" FontStyle="Italic"/>
            <Button x:Name="MyButton" Content="Click me!">
                <i:Interaction.Behaviors>
                    <local:MyBehavior Message="{Binding MyMessage, Source={StaticResource MyResource}}"/>
                </i:Interaction.Behaviors>
            </Button>
        </StackPanel>
    </UserControl>
    


    现在代码有两个类:一个用于行为,另一个用于视图模型:

    public class MyViewmodel
    {
        public string MyMessage
        {
            get { return "Hello, world!"; }
        }
    }
    


    public class MyBehavior : Behavior<Button>
    {
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message",
            typeof(string), typeof(MyBehavior),
            new PropertyMetadata("(no message)"));
    
        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }
    
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Click += new RoutedEventHandler(AssociatedObject_Click);
        }
    
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.Click -= new RoutedEventHandler(AssociatedObject_Click);
        }
    
        void AssociatedObject_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(Message);
        }
    }
    


    AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 15 Position: 43]

    谢谢!


    here


    here ,这个似乎奏效了。这一次,使用的技巧是

    <StackPanel.Resources>
        <local:MyViewmodel x:Key="MyResource"/>
        <local:DeepSetter 
            x:Key="MyBehaviorSetter"
            TargetProperty="Message"
            BindingExport="{Binding MyMessage, Source={StaticResource MyResource}}"/>
    </StackPanel.Resources>
    

    …并按如下方式修改按钮的行为声明:

    <local:MyBehavior local:DeepSetter.BindingImport="{StaticResource MyBehaviorSetter}"/>
    

    好消息:任何DependecyObject的数据绑定都将在Silverlight 4上可用: http://timheuer.com/blog/archive/2009/11/18/whats-new-in-silverlight-4-complete-guide-new-features.aspx#dobind

    1 回复  |  直到 16 年前
        1
  •  1
  •   Joy George Kunjikkuru    16 年前

    为了获得DataBinding支持,类应该从FrameworkElement继承。希望微软支持Silverlight 4