StyleSelector.SelectStyle(Object, DependencyObject) Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
When overridden in a derived class, returns a Style based on custom logic.
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
The content.
- container
- DependencyObject
The element to which the style will be applied.
Returns an application-specific style to apply; otherwise, null
.
The following example shows how to define a StyleSelector that defines a Style for a row. 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
The following example shows how to define a ResourceKey for the StyleSelector. The namespc
prefix maps to a CLR namespace and the corresponding assembly where the StyleSelector is defined. For more information, see XAML Namespaces and Namespace Mapping for WPF XAML.
<namespc:ListViewItemStyleSelector x:Key="myStyleSelector"/>
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>
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.
To create a StyleSelector that applies a style based on custom logic, create a subclass of the StyleSelector class and implement the SelectStyle method.
Produit | Versions |
---|---|
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |
Commentaires sur .NET
.NET est un projet open source. Sélectionnez un lien pour fournir des commentaires :