2017-12-27 321 views
-1

我正在學習WPF和我自己的和 我試圖製作一個使用徽標和標題的遊戲列表。 我遵循我在某些WPF教程中看到的未獲成功的內容。我的模板只顯示空行。當鼠標懸停在某個項目上但沒有顯示任何內容時,我可以看到所有元素都是正確的高度。C#WPF ListView模板沒有渲染任何東西

myListView.ItemsSource被設置爲我的數據庫。我在這個時候正確地收到模型,但正如我所說,沒有什麼是渲染

有人能告訴我我的代碼有什麼問題嗎?

這裏我WPF:

<ListView x:Name="ListGames" Margin="0,57,0,30" Background="#111" BorderThickness="0" ItemsSource="{Binding Path=Game}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Height="200" Orientation="Horizontal"> 
        <StackPanel.Background> 
         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
          <GradientStop Color="Black" Offset="0"/> 
          <GradientStop Color="#333" Offset="1"/> 
         </LinearGradientBrush> 
        </StackPanel.Background> 
        <Image MaxWidth="800" Height="175" Stretch="Fill" Source="{Binding Path=logoPath}"/> 
        <TextBlock Foreground="#ddd" Text="{Binding Path=Title}" /> 
       </StackPanel> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

這裏我的遊戲類:

class Game 
{ 
    public Int32 id; 
    public string title; 
    public string type; 
    public string description; 
    public string logoPath; 

    public Game(Int32 a_id, string a_title, string a_type, string a_description, string a_logoPath) 
    { 
    id = a_id; 
    title = a_title; 
    type = a_type; 
    description = a_description; 
    logoPath = a_logoPath; 
    } 
} 

回答

1

首先,你不能綁定到一個字段。您需要將您的字段更改爲屬性:

public string logoPath { get; set; } 

其次,綁定區分大小寫。如果你不喜歡C#命名約定,你至少需要按照自己的慣例一致:

Text="{Binding Path=title}" 

第三:我可以分析這一點,但我不能把它解釋:

myListView .ItemsSource被設置爲我的數據庫。我在這個時候正確地收到模型,但正如我所說,沒有任何渲染

ListView中是否存在行?

+0

這仍然行不通,除非ListView的DataContext或其父項之一被設置爲持有對Game屬性的引用。對? –

+1

@AndréB對。目前尚不清楚OP是否正在這樣做。我傾向於認爲他是 - 或者他將其分配在代碼後面,而XAML中的綁定只是一個noob no-op。 –

+0

我總有嘗試設想後者的傾向! 只是一個簡單的問題,爲什麼我們需要設置DataContext,只要我們在標記中設置控件的ItemsSource屬性,比如ListView,而不是當我們直接在代碼後面執行時? –