WPF 设计时视图模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15765986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:25:47  来源:igfitidea点击:

WPF Design Time View Model

c#.netwpfsilverlightxaml

提问by Chris

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

回答by TYY

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

有一个 stackoverflow 帖子展示了如何使用 d:designinstance 将设计时间管理添加到您的视图中。一探究竟。

Question about ViewModel Management (DesignTime Vs Run Time)

关于 ViewModel 管理的问题(设计时间与运行时间)

回答by cunningdave

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:

你能分享一下 UnitViewModel 的代码定义吗?请记住,绑定仅适用于属性,不适用于开放字段。我尝试了你的代码并为单位创建了一些基本的结构字段。那些没有用。所以,我猜你可能正在使用字段而不是属性:

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 以正确设置数据表示的样式。