How to get mock data in listview at design time and real data at run time in WPF

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

My data is an observable collection of a simple object such as a model, which provides several properties, such as Id, Title, Description, etc.

At runtime, I need to access the data source collection from the code in order to dynamically change the contents

I currently 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 layout data at runtime, but not at design time. The list is just an empty rectangle in the designer, although I see the text "Hello WPF"

?

Edit:

? , xaml, , , , . , ,

+3
1
<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> 

, datacontext, datacontext , d, . MVVM viewmodels, , . , . Datacontext datatemplating. viewmodel , design-view .

+3

Source: https://habr.com/ru/post/1757480/


All Articles