StyleSelector 类

定义

提供根据自定义逻辑应用样式的方法。Provides a way to apply styles based on custom logic.

public ref class StyleSelector
public class StyleSelector
type StyleSelector = class
Public Class StyleSelector
继承
StyleSelector

示例

下面的示例演示如何定义StyleSelector Style为行定义的。The following example shows how to define a StyleSelector that defines a Style for a row. 此示例根据行Background索引定义颜色。This example defines the Background color according to the row index.

public class ListViewItemStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, 
        DependencyObject container)
    {
        Style st = new Style();
        st.TargetType = typeof(ListViewItem);
        Setter backGroundSetter = new Setter();
        backGroundSetter.Property = ListViewItem.BackgroundProperty;
        ListView listView = 
            ItemsControl.ItemsControlFromItemContainer(container) 
              as ListView;
        int index = 
            listView.ItemContainerGenerator.IndexFromContainer(container);
        if (index % 2 == 0)
        {
            backGroundSetter.Value = Brushes.LightBlue;
        }
        else
        {
            backGroundSetter.Value = Brushes.Beige;
        }
        st.Setters.Add(backGroundSetter);
        return st;
    }
}    
Public Class ListViewItemStyleSelector
    Inherits StyleSelector
    Public Overrides Function SelectStyle(ByVal item As Object, ByVal container As DependencyObject) As Style
        Dim st As New Style()
        st.TargetType = GetType(ListViewItem)
        Dim backGroundSetter As New Setter()
        backGroundSetter.Property = ListViewItem.BackgroundProperty
        Dim listView As ListView = TryCast(ItemsControl.ItemsControlFromItemContainer(container), ListView)
        Dim index As Integer = listView.ItemContainerGenerator.IndexFromContainer(container)
        If index Mod 2 = 0 Then
            backGroundSetter.Value = Brushes.LightBlue
        Else
            backGroundSetter.Value = Brushes.Beige
        End If
        st.Setters.Add(backGroundSetter)
        Return st
    End Function
End Class

下面的示例演示如何为定义ResourceKeyStyleSelectorThe following example shows how to define a ResourceKey for the StyleSelector. 前缀映射到命名空间和定义了的StyleSelector相应程序集。 CLRCLR namespcThe namespc prefix maps to a CLRCLR namespace and the corresponding assembly where the StyleSelector is defined. 有关详细信息, 请参阅WPF xaml 的 XAML 命名空间和命名空间映射For more information, see XAML Namespaces and Namespace Mapping for WPF XAML.

<namespc:ListViewItemStyleSelector x:Key="myStyleSelector"/>

下面的示例演示如何将的ItemContainerStyleSelector属性ListView设置为此StyleSelector资源。The following example shows how to set the ItemContainerStyleSelector property of a ListView to this StyleSelector resource.

<ListView 
      ItemsSource="{Binding Source={StaticResource EmployeeData}, 
                                        XPath=Employee}"
      ItemContainerStyleSelector="{DynamicResource myStyleSelector}" >      
  <ListView.View>
    <GridView>
      <GridViewColumn DisplayMemberBinding="{Binding XPath=FirstName}" 
                      Header="First Name" Width="120"/>
      <GridViewColumn DisplayMemberBinding="{Binding XPath=LastName}" 
                      Header="Last Name" Width="120"/>
      <GridViewColumn DisplayMemberBinding="{Binding XPath=FavoriteCity}" 
                      Header="Favorite City" Width="120"/>
    </GridView>
  </ListView.View>
</ListView>

有关如何创建选择器以选择定义的样式资源的示例, 请参阅的实现DataTemplateSelector.SelectTemplate, 该实现允许你根据类似的概念使用自定义逻辑来DataTemplate选择。For an example of how to create a selector to choose a defined style resource, see the implementation of DataTemplateSelector.SelectTemplate, which allows you to use custom logic to select a DataTemplate, based on a similar concept.

注解

若要创建StyleSelector基于自定义逻辑应用样式的, 请创建StyleSelector类的子类并实现SelectStyle方法。To create a StyleSelector that applies a style based on custom logic, create a subclass of the StyleSelector class and implement the SelectStyle method.

构造函数

StyleSelector()

初始化 StyleSelector 的新实例。Initializes a new instance of a StyleSelector.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
SelectStyle(Object, DependencyObject)

在派生类中重写时,基于自定义逻辑返回 StyleWhen overridden in a derived class, returns a Style based on custom logic.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅