2009-10-07 22 views
1

我試圖確定附加行爲是我們需要在樓宇控制爲我們的應用程序中的表單。我創建了一個附加屬性,現在我該如何使用它?

因此,我不僅要知道如何創建附加行爲,但要看到他們是使用解決問題的實際情況。我用this MSDN article來創建一個帶有附加屬性的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(); 
     } 
    } 
} 

回答

1

這裏是一個真實altough非常簡單的例子:一個附加行爲,顯示了消息,當一個用戶點擊一個按鈕。消息可以通過行爲本身的依賴屬性進行定製。

行爲代碼:

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運行時的一部分,但安裝了微軟的Expression Blend 3,看來無論如何,這個組件可以被自由重新分配(見這裏:http://social.expression.microsoft.com/Forums/en-US/blend/thread/8523aec4-1a10-4864-8ad4-f95a3627bb4a

+0

我需要引用什麼來使用「行爲」類,我只找到System.ComponentModel.CategoryAttribute.Behavior(http://msdn.microsoft.com/en-us/library/system。 System.dll中的componentmodel.categoryattribute.behavior.aspx),我已經引用它說類型「行爲」找不到。 – 2009-10-07 13:36:43

+0

您需要引用System.Windows.Interactivity程序集。它作爲Expression Blend 3的一部分安裝。 – Konamiman 2009-10-07 13:46:28

+0

這在WPF中運行良好,我從技術上理解它是如何工作的。我想我們可以如何在我們的表單中使用它,也許可以代替定義ItemType(客戶或員工)的消息,因此具有不同的驗證,消息等。您如何在現實世界的應用程序中使用它? – 2009-10-07 14:10:35

相關問題