2016-02-19 80 views
0

我想創建一個自定義依賴項屬性,可用於按鈕內容的位置。請幫助我。如何創建按鈕內容,如依賴項屬性

這是我的依賴屬性 -

public class MyControl:Button 
    {   
     public static readonly DependencyProperty MyCustomControlProperty = 
      DependencyProperty.Register("SourceC", typeof(string), typeof(MyControl), new UIPropertyMetadata("my button")); 

     public string SourceC 
     {    
      get { return (string)GetValue(MyCustomControlProperty); } 
      set { SetValue(MyCustomControlProperty, value); }  
     } 

這是我XAML-

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:MyWPF" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="MyWPF.MainWindow" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <local:CustomClass x:Key="MDP"/> 
    <local:CustomProperty x:Key="CP"/> 
</Window.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50"/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="100"/> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions> 
    <local:MyControl x:Name="btn1" SourceC="abc" Click="MyControl_Click" Width="50" Height="30" RenderTransformOrigin="3.74,4.333" VerticalAlignment="Top" Grid.Row="1" Margin="88.5,67,0,0" HorizontalAlignment="Left" Grid.Column="1" d:LayoutOverrides="Width, Height"/> 
</Grid> 


我希望看到按鈕內容,如 「ABC」,我怎麼能用依賴項屬性創建按鈕內容?

+0

您可能需要重新翻譯你的問題AMIGO。當它坐下時,我不明白爲什麼你不會只用一個帶有內容的Button仍然哈哈。 –

回答

0

試試這個

public class MyControl : Button 
{ 
    public static DependencyProperty MyTextBlockProperty = DependencyProperty.Register("MyTextBlock", typeof(TextBlock), typeof(MyControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(MyTextBlock_Changed))); 

    public TextBlock MyTextBlock 
    { 
     get { return (TextBlock)GetValue(MyTextBlockProperty); } 
     set { SetValue(MyTextBlockProperty, value); } 
    } 


    private static void MyTextBlock_Changed(DependencyObject o, DependencyPropertyChangedEventArgs args) 
    { 
     MyControl thisClass = (MyControl)o; 
     thisClass.SetMyTextBlock(); 
    } 

    private void SetMyTextBlock() 
    { 
     //Put Instance MyTextBlock Property Changed code here 
    } 

    public static DependencyProperty SourceCProperty = DependencyProperty.Register("SourceC", typeof(string), typeof(MyControl), new FrameworkPropertyMetadata(new PropertyChangedCallback(SourceC_Changed))); 

    public string SourceC 
    { 
     get { return (string)GetValue(SourceCProperty); } 
     set { SetValue(SourceCProperty, value); } 
    } 


    private static void SourceC_Changed(DependencyObject o, DependencyPropertyChangedEventArgs args) 
    { 
     MyControl thisClass = (MyControl)o; 
     thisClass.SetSourceC(); 
    } 

    private void SetSourceC() 
    { 
     MyTextBlock.Text = SourceC; 
    } 

    public MyControl(): base() 
    { 
     MyTextBlock = new TextBlock(); 
     this.Content = MyTextBlock; 
    } 

}