Search code examples
templatescomboboxsilverlight-5.0implicit

Silverlight 5 : How to stop a Combobox from using implicit data template


Love the new Implicit Data Templates, but I've just run into a problem with them.

My ComboBox is picking a DataTemplate which matches the ItemsSources type instead of following my DisplayMemberPath settings. Is there a way to tell the control to not look for DataTemplates?

     <ComboBox DisplayMemberPath="DTO.Name" SelectedValue="{Binding DefaultModifierGroup, Mode=TwoWay}" 
ItemsSource="{Binding MenuRepository.ModifierGroups, Source={StaticResource Locator}}"/>

Solution

  • I think you can't use DisplayMemberPath anymore. You probably need to created new data template inside that combobox.

    <UserControl x:Class="SilverlightApplication1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        xmlns:local="clr-namespace:SilverlightApplication1"
        d:DesignHeight="300" d:DesignWidth="400">
        <UserControl.Resources>
            <DataTemplate DataType="local:Person">
                <TextBlock Text="{Binding Address}" />
            </DataTemplate>
        </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel>
                <ListBox ItemsSource="{Binding Items}" />
                <ListBox ItemsSource="{Binding Items}" >
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
        </Grid>
    </UserControl>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
    namespace SilverlightApplication1 {
        public partial class MainPage : UserControl {
            public MainPage() {
                this.DataContext = this;
                InitializeComponent();
    
                Items = new List<Person>{
                    new Person() { Name ="Name1", Address ="Address1" },
                    new Person() { Name ="Name2", Address ="Address2" },
                    new Person() { Name ="Name3", Address ="Address3" }
                };
    
            }
    
            public IList<Person> Items { get; set; }
    
        }
    
        public class Person {
            public string Name { get; set; }
            public string Address { get; set; }
        }
    }