StyleSelector.SelectStyle(Object, DependencyObject) 方法

定义

当在派生类中重写时,返回基于自定义逻辑的 Style

public:
 virtual System::Windows::Style ^ SelectStyle(System::Object ^ item, System::Windows::DependencyObject ^ container);
public virtual System.Windows.Style SelectStyle (object item, System.Windows.DependencyObject container);
abstract member SelectStyle : obj * System.Windows.DependencyObject -> System.Windows.Style
override this.SelectStyle : obj * System.Windows.DependencyObject -> System.Windows.Style
Public Overridable Function SelectStyle (item As Object, container As DependencyObject) As Style

参数

item
Object

内容。

container
DependencyObject

将应用样式的元素。

返回

Style

返回要应用的应用程序特定样式;否则返回 null

示例

以下示例演示如何定义 StyleSelector 为行定义一个 Style 。 此示例根据行索引定义 Background 颜色。

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

以下示例演示如何为 ResourceKey .StyleSelector 前缀 namespc 映射到 CLR 命名空间和定义位置的 StyleSelector 相应程序集。 有关详细信息,请参阅 WPF XAML 的 XAML 命名空间和命名空间映射

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

以下示例演示如何将属性ListView设置为ItemContainerStyleSelectorStyleSelector资源。

<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

注解

若要创建基于自定义逻辑应用样式的 StyleSelector 样式,请创建类的 StyleSelector 子类并实现 SelectStyle 该方法。

适用于

另请参阅