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