Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以在样式中创建自己的属性吗?_C#_Xaml - Fatal编程技术网

C# 我可以在样式中创建自己的属性吗?

C# 我可以在样式中创建自己的属性吗?,c#,xaml,C#,Xaml,我想创造一个适合我自己的风格,有可能吗 我有翅膀的款式 <Style x:Key="EditTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> <Setter Property="TextWrapping" Value="Wrap"/> <Setter Property="AcceptsReturn" Value="True"/>

我想创造一个适合我自己的风格,有可能吗

我有翅膀的款式

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

我想添加一个名为“操作”的属性。。。


有人知道这是否可行吗?

如果要向“文本框”添加其他属性,则需要扩展该类,例如:

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>



DependencyProperty是这样,您可以从XAML编辑它,object属性是这样,您可以从C#访问它。

如果要向“TextBox”添加另一个属性,您需要扩展该类,例如:

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>



DependencyProperty是这样的,您可以从XAML编辑它,object属性是这样的,您可以从C#访问它。

AFAIK,这是不可能的。但您可以使用其他现有属性,如
标记
数据上下文
。谢谢BenoitBlanchon@BenoitBlanchon您可以使用附加的property@loetn:对!但您不能“动态”创建自己的附加属性。你需要在代码中定义它们。@BenoitBlanchon的确如此,但我没有读过他的问题好吧,那是不可能的。但您可以使用其他现有属性,如
标记
数据上下文
。谢谢BenoitBlanchon@BenoitBlanchon您可以使用附加的property@loetn:对!但您不能“动态”创建自己的附加属性。你需要在代码中定义它们。@BenoitBlanchon的确如此,但我没有读过他的问题