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的确如此,但我没有读过他的问题