Share via


Style.Setters 属性

定义

获取 SetterEventSetter 对象的集合。

public:
 property System::Windows::SetterBaseCollection ^ Setters { System::Windows::SetterBaseCollection ^ get(); };
public System.Windows.SetterBaseCollection Setters { get; }
member this.Setters : System.Windows.SetterBaseCollection
Public ReadOnly Property Setters As SetterBaseCollection

属性值

SetterEventSetter 对象的集合。 默认值为空集合。

示例

以下示例演示如何使用此属性。

Style style = new Style(typeof(ListBoxItem));
style.Setters.Add(new Setter(ListBoxItem.HorizontalContentAlignmentProperty,
     HorizontalAlignment.Stretch));
ListBox lb = new ListBox();
lb.ItemContainerStyle = style;
ListBoxItem lbi1 = new ListBoxItem();
Button btn = new Button();
btn.Content = "Button as styled list box item.";
lbi1.Content = (btn);
lb.Items.Add(lbi1);
Dim style As Style = New Style()
style.Setters.Add(New Setter(ListBoxItem.HorizontalContentAlignmentProperty, _
     HorizontalAlignment.Stretch))
Dim lb As ListBox = New ListBox()
lb.ItemContainerStyle = style
Dim lbi1 As ListBoxItem = New ListBoxItem()
Dim btn As Button = New Button()
btn.Content = "Button as styled list box item."
lbi1.Content = (btn)
lb.Items.Add(lbi1)

SetterBase向 对象添加子Style级会将其隐式添加到对象的 StyleSetterBaseCollection 将以下内容 EventSetter 隐式添加到 SetterBaseCollection 样式的 中:

<StackPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.EventOvw2"
  Name="dpanel2"
  Initialized="PrimeHandledToo"
>
  <StackPanel.Resources>
    <Style TargetType="{x:Type Button}">
      <EventSetter Event="Click" Handler="b1SetColor"/>
    </Style>
  </StackPanel.Resources>
  <Button>Click me</Button>
  <Button Name="ThisButton" Click="HandleThis">
    Raise event, handle it, use handled=true handler to get it anyway.
  </Button>
</StackPanel>

注解

对象的 Setters 属性 Style 只能包含可 SetterBase分配给 的对象。 SetterBase向 对象添加子Style级会将其隐式添加到对象的 StyleSetterBaseCollection

Setters 显式属性元素用法也相对常见。 这是一种标记样式决策,有时可以帮助区分SettersResources复杂样式中的 和 Triggers 项。 例如:

<Style>
  <Style.Setters>
    <!--one or more SetterBase derived object elements here-->
  </Style.Setters>
</Style>

XAML 属性元素用法

<object>
  oneOrMoreSetters
</object>

XAML 值

oneOrMoreSetters 一个或多个 SetterEventSetter 对象。

适用于

另请参阅