【发布时间】: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