WPF Design Time View Model

好久不见. 提交于 2020-01-03 12:14:19

问题


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
            });
    }        
}

回答1:


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.




回答2:


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)



来源:https://stackoverflow.com/questions/15765986/wpf-design-time-view-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!