简体   繁体   English

WPF设计时间视图模型

[英]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
            });
    }        
}

There is a stackoverflow post that shows how to add design time management to your view using d:designinstance. 有一个stackoverflow帖子,显示如何使用d:designinstance将设计时管理添加到视图中。 Check it out. 看看这个。

Question about ViewModel Management (DesignTime Vs Run Time) 关于ViewModel管理的问题(DesignTime与运行时间)

Can you share the code definition for UnitViewModel? 你能分享UnitViewModel的代码定义吗? Keep in mind that Bindings only work on Properties, not on open Fields. 请记住,Bindings仅适用于Properties,而不适用于打开的Fields。 I tried your code and created some basic struct fields for Units. 我尝试了你的代码并为Units创建了一些基本的struct字段。 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. 添加ItemTemplate以正确设置数据表示的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM