本文介绍了WPF设计时间视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的视图模型,其中包含一个单元列表,这在运行时可以正常显示,但是我希望该列表在设计时显示.根据周围的一些问题,我已经尝试了以下方法,但是它不起作用,有人可以帮忙吗?

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"/>

演示者

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

视图模型

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

推荐答案

您可以共享UnitViewModel的代码定义吗?请记住,绑定仅适用于属性,不适用于打开的字段.我尝试了您的代码,并为Units创建了一些基本的struct字段.那些没用.因此,我猜测您可能是在使用字段而不是属性:

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>

添加一个ItemTemplate来正确设置数据表示的样式.

Add an ItemTemplate to properly style the data representation.

这篇关于WPF设计时间视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 00:57