0

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?

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
  • Nice votedown, thanks. Now I can improve a lot the question with your comments ... ... – Ignacio Soler Garcia May 12 '14 at 18:25
  • 1
    +1 simply because it's annoying to see something downvoted without explanation, so I got your back amigo – Chris W. May 12 '14 at 19:31
  • 1
    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 – Miiite May 13 '14 at 07:09
  • 1
    @Miiite: nice approach! The bad thing is that I need to rebuild to see every change. – Ignacio Soler Garcia May 13 '14 at 08:37
  • So you mean it's "live" when using DesignData ? – Miiite May 13 '14 at 08:51
  • @Miiite: yes it is, so you can see the changes in real-time to adjust the layout with different sizes and so on. Anyway designData seems to be much less powerful than your approach. – Ignacio Soler Garcia May 13 '14 at 09:11
  • @Miite: can you put the comment as an answer so I can accept it? – Ignacio Soler Garcia May 13 '14 at 12:28

1 Answers1

1

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

Miiite
  • 1,517
  • 10
  • 16
  • It still has some problems that I've not addressed yet. Seems that attached properties do not work and the same happens with CollectionViewSources ... did you find these issues? – Ignacio Soler Garcia May 15 '14 at 09:26