2011-08-23 124 views
0

我有一個名爲「BTN1」按鈕的控件,我想改變它的內容通過一個依賴屬性在XAML中,像這樣的:容易依賴項屬性

<UserControl:UserControl1 ButtonContents="Something"/> 

這是我有:

Public Class UserControl1 
    Public Shared ReadOnly ButtonContentsProperty As DependencyProperty = 
     DependencyProperty.Register("ButtonContents", 
            GetType(String), 
            GetType(UserControl.UserControl1)) 

    Public Property ButtonContents() As Boolean 
     Get 
      Return GetValue(ButtonContentsProperty) 
     End Get 
     Set(ByVal value As Boolean) 
      SetValue(ButtonContentsProperty, value) 
     End Set 
    End Property 
End Class 

但是,依賴屬性怎麼知道該怎麼做?

+0

添加PropertyChangedCallback並在靜態cal中lback事件處理程序,您可以調用發件人的私有方法,然後設置「內容」。否則,根據您的情況,您可能會綁定它 –

+1

還有一種方法可以做到這一點(我個人比較喜歡) - 將您的DP綁定到按鈕的Content屬性並使用轉換器:)實際上,您可以使用按鈕方面的行爲可以讓您完全訪問整個按鈕,而不僅僅是其內容屬性。請讓我們知道,我會製作一個小樣本。 – 2011-08-23 12:56:46

+0

@Dmitry你能告訴我一個例子嗎? – Cobold

回答

0

該解決方案基於以下方法 - 按鈕的內容被定義爲屬於按鈕本身的資源。不幸的是,ResourceKey不是一個DP,因此不能綁定,我們創建了一個附屬屬性BindiableResourceKey,它保證了這一點。用戶控件具有一個字符串類型ButtonLook屬性,該屬性包含要用作按鈕內容的資源的名稱。如果您需要實現更復雜的鏈接邏輯,只需擴展附加的屬性值更改處理程序。

下面的代碼:

第一部分 - 用戶控制:

<UserControl x:Class="ButtonContentBinding.AControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" 
      d:DesignWidth="300" 
      xmlns:local="clr-namespace:ButtonContentBinding"> 
    <Grid> 
     <Button local:BindableResourceControl.ResourceKey="{Binding ButtonLook}"> 
      <Button.Resources> 
       <Rectangle x:Key="BlueRectangle" 
        Width="40" Height="40" Fill="Blue" /> 
       <Rectangle x:Key="GreenRectangle" 
        Width="40" Height="40" Fill="Green" /> 
      </Button.Resources> 
     </Button> 
    </Grid> 
</UserControl> 

2部分 - 附加屬性:

public class BindableResourceControl : DependencyObject 
    { 
     public static readonly DependencyProperty ResourceKeyProperty = 
      DependencyProperty.RegisterAttached("ResourceKey", 
      typeof(string), 
      typeof(BindableResourceControl), 
      new PropertyMetadata((x, y) => 
      { 
       ContentControl contentControl = x as ContentControl; 

       if (x != null) 
       { 
        contentControl.Content = contentControl.Resources[y.NewValue]; 
       } 
      })); 

     public static void SetResourceKey(DependencyObject x, string y) 
     { 
      x.SetValue(BindableResourceControl.ResourceKeyProperty, y); 
     } 

     public static string GetResourceKey(DependencyObject x) 
     { 
      return (string)x.GetValue(BindableResourceControl.ResourceKeyProperty); 
     } 
    } 

第3部分 - 消費:

<Window x:Class="ButtonContentBinding.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:ButtonContentBinding"> 
    <Grid> 
     <my:AControl ButtonLook="GreenRectangle" 
       HorizontalAlignment="Left" Margin="0"  
       x:Name="aControl1" VerticalAlignment="Top" 
       Height="200" Width="200" /> 
    </Grid> 
</Window> 
0

當您註冊該屬性時,您還可以定義一個PropertyChangedCallback方法,您可以在該屬性更改時決定執行什麼操作。 更多信息herehere

0

我通常會綁定UserControl的XAML中的值,例如

<UserControl ... 
      Name="control"> 
    <!-- ... --> 
    <Button Content="{Binding ButtonContents, ElementName=control}"/> 
    <!-- ... --> 
</UserControl>