I'm trying to build a MultiColumn ComboBox. I followed this example:
http://zamjad.wordpress.com/2012/08/15/multi-columns-combo-box/
However, I have two problems...
1. It does not show two or three columns in the drop down, it only shows one column.
2. For some reason, when I change the value in the combobox the whole record changes!!
Here is my code:
ICollectionView source { get; set; } public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Data.CollectionViewSource orderViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("orderViewSource"))); orderViewSource.Source = App.StoreDB.getAllOrders(); source = (this.FindResource("orderViewSource") as CollectionViewSource).View; }
public class StoreDB { NorthwindEntities context = new NorthwindEntities(); public ICollection<Order> getAllOrders() { var orders = (from o in context.Orders select o).OrderBy(o => o.OrderID).ToList(); return orders; } }
XAML:
<Window 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" xmlns:NorthWindData="clr-namespace:NorthWindData;assembly=NorthWindData" mc:Ignorable="d" x:Class="WpfMultiColumnComboBoxFromDB.MainWindow" Title="MainWindow" Height="374.539" Width="602.491" Loaded="Window_Loaded"><Window.Resources><CollectionViewSource x:Key="orderViewSource" d:DesignSource="{d:DesignInstance {x:Type NorthWindData:Order}, CreateList=True}"/></Window.Resources><Grid><Grid x:Name="grid1" VerticalAlignment="Top" Margin="152,49,0,0" HorizontalAlignment="Left" DataContext="{StaticResource orderViewSource}"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition Width="Auto"/></Grid.ColumnDefinitions><Label VerticalAlignment="Center" Grid.Row="0" Margin="3" HorizontalAlignment="Left" Grid.Column="0" Content="Order ID:"/><TextBox x:Name="orderIDTextBox" Width="120" VerticalAlignment="Center" Text="{Binding OrderID, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Grid.Row="0" Margin="3" Height="23" HorizontalAlignment="Left" Grid.Column="1" IsEnabled="False"/><Label VerticalAlignment="Center" Grid.Row="1" Margin="3" HorizontalAlignment="Left" Grid.Column="0" Content="Customer ID:"/><TextBox x:Name="customerIDTextBox" Width="120" VerticalAlignment="Center" Text="{Binding CustomerID, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Grid.Row="1" Margin="0,6,-166,4" Height="22" HorizontalAlignment="Right" Grid.Column="1"/><ComboBox x:Name="CustomerIDComboBox" Grid.Column="1" Grid.Row="1" ItemsSource="{Binding}" HorizontalContentAlignment="Stretch"><ComboBox.ItemTemplate><DataTemplate><TextBlock Margin="2" Text="{Binding CustomerID}"/></DataTemplate></ComboBox.ItemTemplate><ComboBox.ItemContainerStyle><Style TargetType="{x:Type ComboBoxItem}"><Setter Property="Template"><Setter.Value><ControlTemplate><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><TextBlock Margin="5" Grid.Column="0" Text="{Binding CustomerID}"/><TextBlock Margin="5" Grid.Column="1" Text="{Binding CompanyName}"/><TextBlock Margin="5" Grid.Column="2" Text="{Binding ContactName}"/></Grid></ControlTemplate></Setter.Value></Setter></Style></ComboBox.ItemContainerStyle></ComboBox><Label VerticalAlignment="Center" Grid.Row="2" Margin="3" HorizontalAlignment="Left" Grid.Column="0" Content="Employee ID:"/><TextBox x:Name="employeeIDTextBox" Width="120" VerticalAlignment="Center" Text="{Binding EmployeeID, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Grid.Row="2" Margin="3" Height="23" HorizontalAlignment="Left" Grid.Column="1"/><Label VerticalAlignment="Center" Grid.Row="3" Margin="3" HorizontalAlignment="Left" Grid.Column="0" Content="Freight:"/><TextBox x:Name="freightTextBox" Width="120" VerticalAlignment="Center" Text="{Binding Freight, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Grid.Row="3" Margin="3" Height="23" HorizontalAlignment="Left" Grid.Column="1"/><Label VerticalAlignment="Center" Grid.Row="4" Margin="3" HorizontalAlignment="Left" Grid.Column="0" Content="Order Date:"/><DatePicker x:Name="orderDateDatePicker" VerticalAlignment="Center" SelectedDate="{Binding OrderDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Grid.Row="4" Margin="3" HorizontalAlignment="Left" Grid.Column="1"/><Label VerticalAlignment="Center" Grid.Row="5" Margin="3" HorizontalAlignment="Left" Grid.Column="0" Content="Required Date:"/><DatePicker x:Name="requiredDateDatePicker" VerticalAlignment="Center" SelectedDate="{Binding RequiredDate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Grid.Row="5" Margin="3" HorizontalAlignment="Left" Grid.Column="1"/></Grid><Button HorizontalAlignment="Left" x:Name="btnFirst" Width="35" Height="23" VerticalAlignment="Top" Click="btnFirst_Click" Content="|<" Margin="369,278,0,0"/><Button HorizontalAlignment="Left" Margin="410,278,0,0" x:Name="btnPrevious" Width="35" Height="23" VerticalAlignment="Top" Click="btnPrevious_Click" Content="<"/><Button Margin="454,278,0,0" x:Name="btnNext" HorizontalAlignment="Left" Width="35" Height="23" VerticalAlignment="Top" Click="btnNext_Click" Content=">"/><Button HorizontalAlignment="Left" Margin="498,278,0,0" x:Name="btnLast" Width="35" Height="23" VerticalAlignment="Top" Click="btnLast_Click" Content=">|"/></Grid></Window>
Any Help will be greatly appreciated...
Guisselle