Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Design Time View Model

I have a simple view model that has a list of Units in it, this shows fine in run time, but I would like the list to show in design time. As per some questions around I have tried the following, but it is not working, can someone please help?

//In resources
<local:MainViewModel x:Key="DesignViewModel"/>

The Presenter

<ItemsControl ItemsSource="{Binding Units}" d:DataContext="{Binding Source={StaticResource DesignViewModel}}" Background="Transparent">

The view model

    public MainViewModel()
    {
        Units = new ObservableCollection<UnitViewModel>();
        Units.Add(new UnitViewModel
        {
            ID = "1",
            Degrees = "80",
            IsMaster = true
        });
        for (int i = 0; i < 10; i++)
            Units.Add(new UnitViewModel
            {
                ID = "2",
                Degrees = "40",
                IsMaster = false
            });
    }        
}
like image 671
Chris Avatar asked Apr 02 '13 13:04

Chris


2 Answers

There is a stackoverflow post that shows how to add design time management to your view using d:designinstance. Check it out.

Question about ViewModel Management (DesignTime Vs Run Time)

like image 150
TYY Avatar answered Nov 10 '22 01:11

TYY


Can you share the code definition for UnitViewModel? Keep in mind that Bindings only work on Properties, not on open Fields. I tried your code and created some basic struct fields for Units. Those didn't work. So, I'm guessing that maybe you're using fields instead of properties:

public class MainViewModel
    {
        public MainViewModel()
        {
            Units = new ObservableCollection<UnitViewModel>();
            Units.Add(new UnitViewModel
            {
                ID = "1",
                Degrees = "80",
                IsMaster = true
            });
            for (int i = 0; i < 10; i++)
                Units.Add(new UnitViewModel
                {
                    ID = "2",
                    Degrees = "40",
                    IsMaster = false
                });
        }

        public ObservableCollection<UnitViewModel> Units {
            get;
            set;
        }
    }


    public struct UnitViewModel
    {
        public string ID { get; set;}
        public string Degrees { get; set;}
        public bool IsMaster { get; set;}

    }

}

I tried this code on my end and had no problems.

<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" mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" d:DesignWidth="704">
    <Window.Resources>
        <local:MainViewModel x:Key="DesignViewModel" />
        <DataTemplate x:Key="DataTemplate2">
            <Grid >
                <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding ID}" VerticalAlignment="Top"/>
            </Grid>
        </DataTemplate>
    </Window.Resources>
    <Grid d:DataContext="{StaticResource DesignViewModel}">
        <ItemsControl HorizontalAlignment="Left" Height="450" VerticalAlignment="Top" Width="632" ItemsSource="{Binding Units}" 
            />
    </Grid>
</Window>

Add an ItemTemplate to properly style the data representation.

like image 34
cunningdave Avatar answered Nov 10 '22 01:11

cunningdave