Click here to Skip to main content
15,896,456 members
Articles / Desktop Programming / WPF

Using a different DataTemple when a WPF ComboBox is expanded

Rate me:
Please Sign up or sign in to vote.
5.00/5 (18 votes)
17 Dec 2009CPOL 74.1K   2.3K   39  
Demonstrates overriding the DataTemplateSelector.SelectTemplate method.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!--Address DataTemplates-->
    <DataTemplate x:Key="AddressComboCollapsed" >
        <StackPanel Width="150" HorizontalAlignment="Stretch" >
            <DockPanel HorizontalAlignment="Stretch">
                <TextBlock Text="{Binding Path=AddressName}" 
                           DockPanel.Dock="Left" 
                />
                <TextBlock Text="{Binding Path=AddressType}" 
                           DockPanel.Dock="Right"
                           HorizontalAlignment="Right"
                />
            </DockPanel>
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="AddressComboExpanded" >
        <GroupBox BorderThickness = "1"
                  Margin = "0,0,0,3"
                  Width = "Auto "
                  HorizontalAlignment = "Stretch"
                  Header = "{Binding Path=AddressType}">
            <StackPanel Margin="3" 
                        HorizontalAlignment="Stretch"
                        MinWidth="250">
                    <TextBlock Text = "{Binding Path=AddressName}"/>

                <TextBlock Text = "{Binding Path=AddressLine1}"
                           Name = "tbAddr1"
                />
                <TextBlock Text = "{Binding Path=AddressLine2}"
                           Name = "tbAddr2"
                />

                <!-- City, State, and ZIP display -->
                <StackPanel Orientation = "Horizontal">
                    <TextBlock Text = "{Binding Path=City}" />
                    <TextBlock Text="," Padding="0,0,5,0"/> <!-- Put a comma between city and state -->
                    <TextBlock Text = "{Binding Path=State}" Padding="0,0,5,0" />
                    <TextBlock Text = "{Binding Path=PostalCode}" />
                </StackPanel>
            </StackPanel>
        </GroupBox>

        <DataTemplate.Triggers>
            <!--If the "AddressLine2" portion of the address is blank, then
				HIDE it (no need to display an extra blank line)
                
                This only works is AddressLine2 = "", not if it's null
            -->
            <DataTrigger Binding = "{Binding Path=AddressLine2}" Value = "">
                <Setter TargetName = "tbAddr2" 
					    Property = "Visibility" 
                        Value = "Collapsed"
                />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
    <!--End Address DataTemplates-->
</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)


Written By
Software Developer
United States United States
I’m a Software Engineer at Microsoft working on the Azure Portal. Before that I spent about 20 years developed various business applications at a number of different companies. I have a passion for writing clean, scalable code and sharing what I’ve learned with others.

I also help run the Casco Bay .Net User Group

Comments and Discussions