簡體   English   中英

我創建了一個附加屬性,現在如何使用它?

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

我試圖確定“附加行為”是否是我們為應用程序中的表單構建控件時需要的東西。

因此,我不僅想知道如何創建“附加行為”,而且還希望看到用於解決問題的真實實例。 我使用這篇MSDN文章以一種我認為有用的方式創建了一個帶有附加屬性的UserControl,即,其中某些子元素被突出顯示或未突出顯示的堆棧面板。

這個示例運行良好,但是我該在邏輯上高亮顯示元素還是不高亮顯示(例如更改背景顏色)?

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>

背后的代碼:

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

這是一個真正的非常簡單的示例:一種附加行為,當用戶單擊按鈕時會顯示一條消息。 可以通過行為本身中的依賴項屬性來定制消息。

行為代碼:

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

使用行為的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>

它們的關鍵是您必須重寫OnAttachedOnDetached方法。 在這里,您可以將處理程序附加/分離到要在關聯元素中控制的任何事件,並在處理程序中執行所需的任何操作。 這是將交互性添加到可視控件的一種非常強大且靈活的方法。

更新資料

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM