繁体   English   中英

我可以创建自己的样式属性吗?

[英]Can I create my own properties in styles?

我想用一种样式正确地创建自己的样式,可以吗?

我有偏执的风格

<Style x:Key="EditTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="TextWrapping" Value="Wrap"/>
    <Setter Property="AcceptsReturn" Value="True"/>
    <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
</Style>

我想添加一个名为“ OPERATION”的属性...

有人知道这是否可能吗?

如果要将另一个属性添加到“文本框”,则需要扩展该类,例如:

public class CustomTextBox : TextBox
{
    public static readonly DependencyProperty OperationProperty = 
        DependencyProperty.Register(
            "Operation", //property name
            typeof(string), //property type
            typeof(CustomTextBox), //owner type
            new FrameworkPropertyMetadata("Default value")
        );
    public string Operation
    {
        get
        {
            return (string)GetValue(OperationProperty);
        }
        set
        {
            SetValue(OperationProperty, value);
        }
    }
}

然后,您可以设置自定义文本框样式:

<Style x:Key="EditTextBox" TargetType="{x:Type CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Operation" Value="string value"/>
</Style>

要么

<my:CustomTextBox Operation="My value" Text="You can still use it as a textbox" />

DependencyProperty是,因此您可以从XAML编辑它,而object属性是,则可以从C#访问它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM