Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

WPF Introduction: Databinding + Styles + IValueConverter

, 5 Aug 2009
A small introduction about Listview Databinding and Styles with introduction to IValueConverter interface
ListViewDataBindingSample.zip
ListViewDataBindingSample
ListViewDataBindingSample
bin
Debug
ListViewDataBindingSample.exe
ListViewDataBindingSample.vshost.exe
Properties
Settings.settings
ListViewDataBindingSample.suo
ListViewDataBindingSample.vshost.exe.manifest
<ResourceDictionary
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 xmlns:appStyles="clr-namespace:ListViewDataBindingSample">

    <!-- ListViewItem Alternate colour template-->
    <appStyles:BackgroundConverter x:Key="AlternateBackgroundConverter"/>


    <Style x:Key="ListViewAlternateItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Background">
            <Setter.Value>
                <Binding RelativeSource="{RelativeSource Self}" 
                 Converter="{StaticResource AlternateBackgroundConverter}"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="Border" Margin="-3,0,1,0" Padding="0,3,0,3"
                  SnapsToDevicePixels="True" Background="{TemplateBinding Background}">
                        <StackPanel Orientation="Horizontal">
                            <GridViewRowPresenter VerticalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Border" Property="Background">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFD4DFFE" Offset="0"/>
                                        <GradientStop Color="#FFE0E4EF" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter TargetName="Border" Property="CornerRadius" Value="2"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#888"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>



</ResourceDictionary>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sathishkumar_P
Software Developer (Senior)
India India
Working in a software development company specializing financial products as Senior Developer. Interested in WPF, WCF, C# and C.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 6 Aug 2009
Article Copyright 2009 by Sathishkumar_P
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid