简体   繁体   English

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

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

I want to create my own properly in a style, is it possible? 我想用一种样式正确地创建自己的样式,可以吗?

I have the foliwing style 我有偏执的风格

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

and I want to add a property that is called 'OPERATION'... 我想添加一个名为“ OPERATION”的属性...

Anybody know if that is possible? 有人知道这是否可能吗?

If you want to add another property to a 'TextBox' you need to extend the class, for example: 如果要将另一个属性添加到“文本框”,则需要扩展该类,例如:

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

Then you can set your custom textbox style: 然后,您可以设置自定义文本框样式:

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

Or 要么

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

The DependencyProperty is so you can edit it from XAML and the object property is so you can access it from C#. DependencyProperty是,因此您可以从XAML编辑它,而object属性是,则可以从C#访问它。

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

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