如何在WPF运行时获得在设计时模拟数据转换成ListView和真实数据 [英] How to get mock data into listview during design time and real data at run time in WPF

查看:116
本文介绍了如何在WPF运行时获得在设计时模拟数据转换成ListView和真实数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2008中写一个WPF应用程序。我是新来WPF和希望能够看到我在设计时的ListView的内容,以便我可以看到我在XAML正在做的,但在运行时绑定到我的真实数据。

我的数据是公开像ID,标题,描述等几个属性的简单模式类对象的观察集合

在运行时我需要能够获得在从code中的数据源收集,所以我可以动态改变的内容。

目前我有:

 <窗​​口x:类=EktronDataUI.Window1
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    XMLNS:地方=CLR的命名空间:EktronDataUI
    标题=窗口1HEIGHT =300WIDTH =300>
    < Window.Resources>
        < ObjectDataProvider的对象类型={X:类型本地:SmartFormDefinitionProvider}X:键=formsProvider方法名=GetMockData/>
    < /Window.Resources>
    <网格和GT;
        <&DockPanel中GT;
            < TextBlock的DockPanel.Dock =评出的>您好WPF< / TextBlock的>
            < ListView控件名称=myListView的ItemsSource ={绑定源= {StaticResource的formsProvider}}>
                < ListView.View>
                    <&GridView的GT;
                        < GridViewColumn标题=IDDisplayMemberBinding ={绑定ID}/>
                        < GridViewColumn标题=标题DisplayMemberBinding ={结合标题}/>
                    < / GridView的>
                < /ListView.View>
            < /&的ListView GT;
        < / DockPanel中>
    < /网格和GT;
< /窗GT;

这我显示我的模拟数据在运行时,而不是在设计时。 ListView控件只是在设计一个空白的矩形,虽然我看到你好WPF文本

请告诉我处理这种典型的方式是什么?

编辑:

这应该告诉我我的数据在设计时,因为它代表?我发现,如果我切XAML的列表视图,然后将其粘贴回,我看到我的数据,它不完全显示正确的,但它的存在。但我建立它的那一刻消失,犯规回来


解决方案

 <窗​​口x:类=EktronDataUI.Window1
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
的xmlns:MC =htt​​p://schemas.openxmlformats.org/markup-compatibility/2006
的xmlns:D =htt​​p://schemas.microsoft.com/ex$p$pssion/blend/2008
XMLNS:地方=CLR的命名空间:EktronDataUI
的xmlns:design_vm =CLR的命名空间:Company.Product.ViewModel.Design
MC:可忽略=D
标题=窗口1HEIGHT =300WIDTH =300>
< Window.Resources>
    < ObjectDataProvider的对象类型={X:类型design_vm:MyListViewModel}X:键=DesignTime_DataSourceD:IsDataSource =真/>
< /Window.Resources>
<电网D:的DataContext ={StaticResource的DesignTime_DataSource}>
    <&DockPanel中GT;
        < TextBlock的DockPanel.Dock =评出的>您好WPF< / TextBlock的>
        < ListView控件名称=myListView的ItemsSource ={绑定路径= ListItems的>
            < ListView.View>
                <&GridView的GT;
                    < GridViewColumn标题=IDDisplayMemberBinding ={绑定ID}/>
                    < GridViewColumn标题=标题DisplayMemberBinding ={结合标题}/>
                < / GridView的>
            < /ListView.View>
        < /&的ListView GT;
    < / DockPanel中>
< /网格和GT;

提供您所使用的的DataContext类的子类,并把它挂起来作为设计时的datacontext使用d命名空间在我的例子。我有MVVM项目,为我的ViewModels一个命名空间,每个类都有,我使用设计的子类。这些子类有填充他们的设计时间数据的构造。运行时DataContext的可以在codebehind或通过datatemplating进行设置。该视图模型已经为ListView项目一个ObservableCollection,并在设计视图模型这是在构造函数中充满了样本数据。

I am using Visual Studio 2008 writing a WPF application. I am new to WPF and would like to be able to see the contents of my listview at design time so that I can see what I am doing in the xaml, but bind to my real data at run time.

My data is an observable collection of a simple model type object that exposes a few properties like Id, Title, Description, etc

At runtime I need to be able to get at the data source collection from the code so I can change the contents dynamically

Currently I have:

<Window x:Class="EktronDataUI.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:EktronDataUI"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider ObjectType="{x:Type local:SmartFormDefinitionProvider}" x:Key="formsProvider" MethodName="GetMockData" />
    </Window.Resources>
    <Grid>
        <DockPanel>
            <TextBlock DockPanel.Dock="Top">Hello WPF</TextBlock>
            <ListView Name="myListView" ItemsSource="{Binding Source={StaticResource formsProvider}}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}" />
                        <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" />
                    </GridView>
                </ListView.View>
            </ListView>
        </DockPanel>
    </Grid>
</Window>

Which shows me my mock data at runtime, but not at design time. The listview is just a blank rectangle in the designer, though I do see the "Hello WPF" text

Whats the typical way of handling this?

Edit:

Should this show me my data at design time as it stands? I found that if I cut the listview from xaml and then paste it back in, I see my data, its not exactly displayed right, but its there. But the moment I build it disappears and doesnt come back

解决方案

<Window x:Class="EktronDataUI.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:local="clr-namespace:EktronDataUI" 
xmlns:design_vm="clr-namespace:Company.Product.ViewModel.Design"
mc:Ignorable="d" 
Title="Window1" Height="300" Width="300"> 
<Window.Resources> 
    <ObjectDataProvider ObjectType="{x:Type design_vm:MyListViewModel}" x:Key="DesignTime_DataSource" d:IsDataSource="True"/>
</Window.Resources> 
<Grid d:DataContext="{StaticResource DesignTime_DataSource}> 
    <DockPanel> 
        <TextBlock DockPanel.Dock="Top">Hello WPF</TextBlock> 
        <ListView Name="myListView" ItemsSource="{Binding Path=ListItems"> 
            <ListView.View> 
                <GridView> 
                    <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}" /> 
                    <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" /> 
                </GridView> 
            </ListView.View> 
        </ListView> 
    </DockPanel> 
</Grid> 

Provide a subclass of the class you're using as datacontext and hook it up as a design time datacontext using the d namespace as in my example. I have MVVM project with a namespace for my viewmodels and each class has a subclass that I use for design. These subclasses have constructors that populate them with design time data. The datacontext for runtime can be set in codebehind or through datatemplating. The viewmodel has an observablecollection for the listview items, and in the design-viewmodel this is filled with sample data in the constructor.

这篇关于如何在WPF运行时获得在设计时模拟数据转换成ListView和真实数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆