2009-02-23 7 views
1

Le code suivant me donne l'erreur (impossible d'ajouter le type Object to Stackpanel).Comment puis-je dire .ToString() en XAML?

Comment puis-je dire .ToString() en XAML?

<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> 

obligatoire dans le code-behind avec ADO.NET Entity Framework:

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

Répondre

1

Vous devez définir la propriété ContentTemplate, non Content.

Essayez:

<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> 

Voir this article

+0

excellent, cela fonctionne, savez-vous comment je peux utiliser StringFormat dans ce contexte? http://stackoverflow.com/questions/577697/how-can-i-get-multibinding-to-work-in-xaml-listbox –

Questions connexes