StyleSelector 클래스

정의

사용자 지정 논리에 따라 스타일을 적용하는 방법을 제공합니다.

public ref class StyleSelector
public class StyleSelector
type StyleSelector = class
Public Class StyleSelector
상속
StyleSelector

예제

다음 예제에서는 정의 하는 방법을 보여 줍니다는 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 매핑됩니다. 자세한 내용은 XAML 네임 스페이스 및 WPF XAML에 대 한 매핑 Namespace합니다.

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

다음 예제에서는 설정 하는 방법을 보여 줍니다 합니다 ItemContainerStyleSelector 의 속성을 ListViewStyleSelector 리소스입니다.

<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 메서드.

생성자

StyleSelector()

StyleSelector 클래스의 새 인스턴스를 초기화합니다.

메서드

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
SelectStyle(Object, DependencyObject)

파생 클래스에서 재정의된 경우 사용자 지정 논리에 따라 Style 을 반환합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보