If you need to get data into a ComboBox one click on an empty line, I suggest you use Caliburn.Micro to βattachβ the command to the DropDownOpened event of your ComboBox .
Here's a sample: primarily XAML
<Window x:Class="WpfApplication1.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cal="http://www.caliburnproject.org" Title="MainView" Height="600" Width="600" Name="_window"> <DataGrid GridLinesVisibility="All" AutoGenerateColumns="False" ItemsSource="{Binding TestItemCollection}"> <DataGrid.Columns> <DataGridTemplateColumn Width="*" Header="Test Column"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox Width="150" HorizontalAlignment="Left" ItemsSource="{Binding TestChildCollection}" cal:Message.Attach="[Event DropDownOpened] = [Action Choose($dataContext)]"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
Then ViewModel:
public class MainViewModel : Caliburn.Micro.PropertyChangedBase { public MainViewModel() { TestItemCollection = new ObservableCollection<TestClass> { new TestClass(), new TestClass(), new TestClass(), }; } public void Choose(object data) { if (!(data is TestClass)) { TestItemCollection.Add(new TestClass()); } } public ObservableCollection<TestClass> TestItemCollection { get; set; } }
Please note that in my example, the TestClass code TestClass same as the one you wrote. Of course, you must configure the application to work with Caliburn.Micro (if you do not know how to do this, you can read the documentation ).
If you don't want (or maybe can't) use Caliburn.Micro, you can get the same result using System.Windows.Interactivity (see my edit below).
Try the code: just a click and a new line is automatically created. Hope this helps you.
EDIT: Alternative Solution with System.Windows.Interactivity
If you cannot use Caliburn.Micro, you just need to modify the MainView XAML in this way:
<Window x:Class="WpfApplication1.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" Title="MainView" Height="600" Width="600" Name="_window"> <DataGrid GridLinesVisibility="All" AutoGenerateColumns="False" ItemsSource="{Binding TestItemCollection}"> <DataGrid.Columns> <DataGridTemplateColumn Width="*" Header="Test Column"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox Width="150" HorizontalAlignment="Left" ItemsSource="{Binding TestChildCollection}"> <i:Interaction.Triggers> <i:EventTrigger EventName="DropDownOpened"> <ei:CallMethodAction MethodName="ChooseWithInteraction" TargetObject="{Binding ElementName=_window, Path=DataContext}" /> </i:EventTrigger> </i:Interaction.Triggers> </ComboBox> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Window>
As you can see, I just added links to the Microsoft.Expression.Interactions and System.Windows.Interactivity libraries. Then I added EventTrigger and CallMethodAction to the ComboBox .
Now in MainViewModel you can replace the Choose method with ChooseWithInteraction one (of course, you can also just add it to the code):
public void ChooseWithInteraction(object sender, EventArgs args) { object data = ((ComboBox)sender).DataContext; if (!(data is TestClass)) { TestItemCollection.Add(new TestClass()); } }
That way you can get the same behavior of my first solution, but without using Caliburn.