【问题标题】:How to Create Button Content like dependency property如何创建类似依赖属性的按钮内容
【发布时间】:2016-02-19 20:59:01
【问题描述】:

我想创建一个自定义依赖属性,可以用来代替按钮内容。请帮助我。

这是我的依赖属性-

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”这样的按钮内容,如何创建具有依赖属性的按钮内容?

【问题讨论】:

  • 您可能需要重新翻译您的问题。就目前而言,我不明白你为什么不只使用内容仍然是 lol 的按钮。

标签: wpf xaml dependency-properties


【解决方案1】:

试试这个

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多