简体   繁体   English

我创建了一个附加属性,现在如何使用它?

[英]I've created an Attached Property, now how do I use it?

I'm trying to determine if Attached Behaviors are something we need in building controls for the forms in our application. 我试图确定“附加行为”是否是我们为应用程序中的表单构建控件时需要的东西。

Hence, I not only want to know how to create Attached Behaviors but want to see real instances in which they are used to solve problems. 因此,我不仅想知道如何创建“附加行为”,而且还希望看到用于解决问题的真实实例。 I used this MSDN article to create a UserControl with an attached property in a way that I think would be useful, namely, a stackpanel in which certain child elements are either highlighted or not highlighted. 我使用这篇MSDN文章以一种我认为有用的方式创建了一个带有附加属性的UserControl,即,其中某些子元素被突出显示或未突出显示的堆栈面板。

This example runs fine, but where do I put the logic to highlight or not highlight (eg change the background color) of the child elements? 这个示例运行良好,但是我该在逻辑上高亮显示元素还是不高亮显示(例如更改背景颜色)?

XAML: XAML:

<Window x:Class="TestAttachedProperties2343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestAttachedProperties2343"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <local:ExtendedStackPanel>
            <TextBlock local:ExtendedStackPanel.IsHighlighted="True" Text="text1"/>
            <TextBox local:ExtendedStackPanel.IsHighlighted="False" Text="text2"/>
            <TextBox local:ExtendedStackPanel.IsHighlighted="True" Text="text3"/>
        </local:ExtendedStackPanel>
    </Grid>
</Window>

Code Behind: 背后的代码:

using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System;

namespace TestAttachedProperties2343
{
    public partial class ExtendedStackPanel : StackPanel
    {
        public static readonly DependencyProperty IsHighlightedProperty = DependencyProperty.RegisterAttached(
            "IsHighlighted",
            typeof(Boolean),
            typeof(ExtendedStackPanel),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));

        public static void SetIsHighlighted(UIElement element, Boolean value)
        {
            element.SetValue(IsHighlightedProperty, value);
        }
        public static Boolean GetIsHighlighted(UIElement element)
        {
            return (Boolean)element.GetValue(IsHighlightedProperty);
        }

        public ExtendedStackPanel()
        {
            InitializeComponent();
        }
    }
}

Here is a real altough very simple example: an attached behavior that shows a message when a user clicks a button. 这是一个真正的非常简单的示例:一种附加行为,当用户单击按钮时会显示一条消息。 The message can be customized via a dependency property in the behavior itself. 可以通过行为本身中的依赖项属性来定制消息。

The behavior code: 行为代码:

public class MyBehavior : Behavior<Button>
{
    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("Message",
        typeof(string), typeof(MyBehavior),
        new PropertyMetadata("Default message"));

    public MyBehavior()
        : base()
    { }

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

The XAML that uses the behavior: 使用行为的XAML:

<Button x:Name="MyButton" Content="Click me for custom behavior">
    <i:Interaction.Behaviors>
        <local:MyBehavior Message="Hello from custom behavior"/>
    </i:Interaction.Behaviors>
</Button>

They key here is that you must override the OnAttached and OnDetached methods. 它们的关键是您必须重写OnAttachedOnDetached方法。 Here you attach/detach handlers to whatever events you want to control in the associated element, and do whatever you want in the handlers. 在这里,您可以将处理程序附加/分离到要在关联元素中控制的任何事件,并在处理程序中执行所需的任何操作。 It is a very powerful and flexible way for adding interactivity to visual controls. 这是将交互性添加到可视控件的一种非常强大且灵活的方法。

Update 更新资料

The base Behavior<> class is in the System.Windows.Interactivity.dll assembly which is not part of the Silverligh runtime, but is installed with Microsoft Expression Blend 3. It seems anyway that this assembly can be freely redistributed (see here: http://social.expression.microsoft.com/Forums/en-US/blend/thread/8523aec4-1a10-4864-8ad4-f95a3627bb4a ) Behavior<>基类位于System.Windows.Interactivity.dll程序集中,该程序集不是Silverligh运行时的一部分,但与Microsoft Expression Blend 3一起安装。似乎无论如何该组件都可以自由地重新分发(请参见此处: http ://social.expression.microsoft.com/Forums/zh-CN/blend/thread/8523aec4-1a10-4864-8ad4-f95a3627bb4a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何绑定到我创建的字符串列表? - How do I bind to a list of string I've created? 为什么即使我已安装相机也未安装相机? - Why is there no camera attached, even if I've attached it? 我在JIT / CLR中发现了一个错误-现在我该如何调试或重现它? - I've found a bug in the JIT/CLR - now how do I debug or reproduce it? 如何通过两种方式将Parent UIElement绑定到附加属性? - How do I two way bind a Parent UIElement to an attached property? 如何以编程方式读取附加依赖属性的值? - How do I programmatically read the value of an attached dependency property? 如何为自定义附加属性提供元素集合? - How do I provide a collection of elements to a custom attached property? 如何使用 C# 按日期升序对创建的数据表进行排序 - How do I sort a datatable i've created in to ascending order of date using C# 如何检测连接到系统的设备何时可以使用? - How do I detect when a device attached to the system is ready for use? Window.FindName找不到我通过附加属性命名的Border元素 - Window.FindName can't find Border element that I've named through an attached property 我已经使用Visual Studio为C#程序创建了一个数据库表。 怎么办? - I've created a database table using Visual Studio for my C# program. Now what?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM