共用方式為


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

下列範例示範如何定義 ResourceKeyStyleSelector 。 前置 namespc 詞會對應至 CLR 命名空間和定義 所在的 StyleSelector 對應元件。 如需詳細資訊,請參閱 WPF XAML 的 XAML 命名空間和命名空間對應

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

下列範例示範如何將 的 ListView 屬性設定 ItemContainerStyleSelector 為此 StyleSelector 資源。

<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 方法。

適用於

另請參閱