Reputation: 21855
I'm doing an application in WPF and I'm using DesignData to speed up the UI creation process as starting the application is a slow process.
I cannot find a way to set the SelectedItem of a combobox with the designdata. Check the following example:
The XAML:
<Window x:Class="DesignDataTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DataContext="{d:DesignData Source=SampleData.xaml}" >
<ComboBox ItemsSource="{Binding GroupingViewModel.Items}" SelectedItem="{Binding GroupingViewModel.SelectedItem}" DisplayMemberPath="Description" Width="100" Height="30" />
</Window>
The ViewModel:
public class MainViewModel
{
public GroupingViewModel GroupingViewModel { get; private set; }
public MainViewModel()
{
this.GroupingViewModel = new GroupingViewModel();
}
}
public class GroupingViewModel
{
public List<GroupingViewModel> Items { get; private set; }
public GroupingViewModel SelectedItem { get; set; }
public string Description { get; set; }
public GroupingViewModel()
{
this.Items = new List<GroupingViewModel>();
}
}
And the designdata:
<designDataTest:MainViewModel.GroupingViewModel>
<designDataTest:GroupingViewModel Description="1">
<designDataTest:GroupingViewModel.Items>
<designDataTest:GroupingViewModel Description="1" />
<designDataTest:GroupingViewModel Description="2" />
<designDataTest:GroupingViewModel Description="3" />
</designDataTest:GroupingViewModel.Items>
<designDataTest:GroupingViewModel.SelectedItem>
<designDataTest:GroupingViewModel Description="1" />
</designDataTest:GroupingViewModel.SelectedItem>
</designDataTest:GroupingViewModel>
</designDataTest:MainViewModel.GroupingViewModel>
</designDataTest:MainViewModel>
Seems that the combobox is expecting to receive an element from the available items collection but I don't know how to reference one of those elements from the designdata file.
Any hint?
Upvotes: 0
Views: 110
Reputation: 1557
Do you really want to use DesignData ? To be honest I never used it this way. From the top of my head, the way I would do this is more to create a "MockMainViewModel" class that will be a class deriving from "MainViewModel", and in the constructor of this MockMainViewModel, you will be able to add any item you want to your collection, and therefore to set your GroupingViewModel.SelectedItem property. Then just set the d:DataContext property to a new instance of your newly created MockMainViewModel
Upvotes: 1