2009-02-23 39 views
1

下面的代碼給了我錯誤(不能將類型Object添加到Stackpanel)。如何在XAML中說.ToString()?

如何在XAML中說.ToString()?

<Window.Resources> 
    <Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Content"> 
      <Setter.Value> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding Path=FirstName}"/> 
       </StackPanel> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 
<Grid> 
    <ListBox x:Name="theCustomers"/> 
</Grid> 

與ADO.NET實體框架綁定後臺代碼:

MainEntities db = new MainEntities(); 
var customers = from c in db.CustomersSet 
       select c; 
theCustomers.ItemsSource = customers; 

回答

1

您需要設置該屬性ContentTemplate,不Content

嘗試:

<Setter Property="ContentTemplate" > 
    <Setter.Value> 
     <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Path=FirstName}"/> 
      <TextBlock Text=" "/> 
      <TextBlock Text="{Binding Path=LastName}"/> 
     </StackPanel> 
     </DataTemplate> 
    </Setter.Value> 
</Setter> 

this article

+0

優秀,這樣的作品,你知道我怎麼能在這種情況下使用的StringFormat? http://stackoverflow.com/questions/577697/how-can-i-get-multibinding-to-work-in-xaml-listbox – 2009-02-23 13:52:03