2016-08-10 5 views
0

デザインビューにDog、Cat、およびHorseが表示されず、リストビューに3つの0とCommonNameが表示されている理由を理解できますか?このリストビューには、デザインビューでは機能していませんが、アプリだけで正しいデータが入力されています。コレクションアイテムがデザインビューに表示されない

ここ enter image description here

のViewModelコードのように見えるものです:

public class MainPageViewModel : ViewModelBase 
{ 
    public ObservableCollection<Animal> Animals { get; } = new ObservableCollection<Animal>(); 
    public MainPageViewModel() 
    { 
     SQLiteConnection db = new SQLiteConnection("Animals.sqlite"); 

     db.CreateTable<Animal>(); 

     if (db.Table<Animal>().Count() == 0) 
     { 
      initCollection(); 
     } 
    } 

    private void initCollection() 
    { 
     Animals.Clear(); 

     Animals.Add(new Animal() { ID = 0, CommonName = "Dog" }); 
     Animals.Add(new Animal() { ID = 1, CommonName = "Cat" }); 
     Animals.Add(new Animal() { ID = 2, CommonName = "Horse" }); 
    } 
} 

Animalクラス:XAMLで

public class Animal 
{ 
    [PrimaryKey] 
    public int ID { get; set; } 
    public string CommonName { get; set; } 
} 

がViewModelに

xmlns:vm="using:Module3_SQLite.ViewModels" 
への参照が設定されています

私は、その後のDataContext

<Page.DataContext> 
    <vm:MainPageViewModel /> 
</Page.DataContext> 

を設定し、ここではリストビュー

<ListView x:Name="lvCollectionItems" 
      ItemsSource="{Binding Animals}" 
      HorizontalAlignment="Left" 
      Width="500" Height="200" Margin="0,10,0,10"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding ID}" Margin="0,0,40,0" /> 
       <TextBlock Text="{Binding CommonName}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

はここであなたがそれを必要とする場合は、完全なページXAMLだだ:

<Page x:Class="Module3_SQLite.Views.MainPage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:Core="using:Microsoft.Xaml.Interactions.Core" 
     xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" 
     xmlns:controls="using:Template10.Controls" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:vm="using:Module3_SQLite.ViewModels" 
     xmlns:local="using:Module3_SQLite.Views" 
     mc:Ignorable="d" RequestedTheme="Dark"> 

    <Page.DataContext> 
     <vm:MainPageViewModel /> 
    </Page.DataContext> 

    <RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

     <controls:PageHeader x:Name="pageHeader" Content="Main Page" 
          RelativePanel.AlignLeftWithPanel="True" 
          RelativePanel.AlignRightWithPanel="True" 
          RelativePanel.AlignTopWithPanel="True" /> 

     <Grid 
      RelativePanel.Below="pageHeader" 
      RelativePanel.AlignLeftWithPanel="True" 
      RelativePanel.AlignRightWithPanel="True" 
      Padding="10"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="2*" /> 
       <ColumnDefinition /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition /> 
       <RowDefinition /> 
      </Grid.RowDefinitions> 

      <StackPanel Grid.Row="0"> 
       <TextBlock Text="Collection Items" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" /> 
       <ListView x:Name="lvCollectionItems" 
          ItemsSource="{Binding Animals}" 
          HorizontalAlignment="Left" 
          Width="500" Height="200" Margin="0,10,0,10"> 
        <ListView.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding ID}" Margin="0,0,40,0" /> 
           <TextBlock Text="{Binding CommonName}" /> 
          </StackPanel> 
         </DataTemplate> 
        </ListView.ItemTemplate> 
       </ListView> 
       <StackPanel Orientation="Horizontal"> 
        <Button x:Name="btnDeleteSelected" Content="Delete Selected" Margin="0,0,10,0" /> 
        <Button x:Name="btnClearList" Content="Clear List" Margin="0,0,10,0" /> 
        <Button x:Name="btnLoadFromDB" Content="Load From DB" Margin="0,0,10,0" /> 
        <Button x:Name="btnResetCollection" Content="Reset Collection" /> 
       </StackPanel> 
      </StackPanel> 

      <StackPanel Grid.Row="1"> 
      <TextBlock Text="Database Items" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" /> 
       <ListView x:Name="lvDatabaseItems" Width="500" Height="200" Margin="0,10,0,10"> 

       </ListView> 
       <StackPanel Orientation="Horizontal"> 
        <Button x:Name="btnSaveToDB" Content="Save Collection to DB" Margin="0,0,10,0" /> 
        <Button x:Name="btnDeleteSelectedFromDB" Content="Delete Selected From DB" Margin="0,0,10,0" /> 
        <Button x:Name="btnDeleteAllFromDB" Content="Delete All From DB" Margin="0,0,10,0" /> 
       </StackPanel> 
      </StackPanel> 

      <StackPanel Grid.Column="1" Grid.RowSpan="2" Orientation="Vertical"> 
       <TextBlock Text="Edit Selected Item" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" /> 
       <StackPanel Orientation="Horizontal" Margin="0,10,0,10"> 
        <TextBlock Text="Name:" FontSize="{ThemeResource TextStyleLargeFontSize}" Width="70"/> 
        <TextBox x:Name="txtbName" Width="200" /> 
       </StackPanel> 
       <StackPanel Orientation="Horizontal" Margin="0,0,0,50"> 
        <TextBlock Text="ID:" FontSize="{ThemeResource TextStyleLargeFontSize}" Width="70" /> 
        <TextBox x:Name="txtbId" Width="200" /> 
       </StackPanel> 

       <Button x:Name="btnSaveToCollection" Content="Save To Collection" Margin="0,0,0,10" /> 
       <Button x:Name="btnInsertToDB" Content="Insert into DB" Margin="0,0,0,10" /> 
       <Button x:Name="btnUpdateDB" Content="Update in DB" Margin="0,0,0,50" /> 

       <TextBlock Text="Modify Collection" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" /> 
       <StackPanel Orientation="Horizontal" Margin="0,10,0,10" > 
        <TextBlock Text="Name:" Width="70" /> 
        <TextBox x:Name="txtbItemNameCollection" Width="200"/> 
       </StackPanel> 
       <StackPanel Orientation="Horizontal" Margin="0,0,0,10"> 
        <TextBlock Text="ID:" Width="70" /> 
        <TextBox x:Name="txtbItemIDCollection" Width="200" /> 
       </StackPanel> 
       <Button x:Name="btnUpdateCollection" Content="Update" HorizontalAlignment="Center" Width="115" /> 
      </StackPanel> 
     </Grid> 
    </RelativePanel> 
</Page> 

答えて

0

を一覧表示するデータソースを追加しましたプログラムで表示しますか?設計者は、バインドされたデータの中に何があるかを見ることができ、設計ビューを介してこのデータを渡すことができます。データソースでコントロールをバインドするときはいつでも、コントロールのポップアップメニューでデータを見たときにデザインビューにデータが表示されました。デザイナーは、デザイナーがそれが接続されていない限り(あなたがアプリケーションを実行してdbからデータを取得するときに、デザイナーにとっては遅すぎる)、ソースの後ろにあるデータを知ることができないからです。

関連する問題