2017-12-27 324 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,而不是当我们直接在代码后面执行时? –