tags:

views:

186

answers:

1

The following code gives me the error (cannot add type Object to Stackpanel).

How can I say .ToString() in 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>

binding in code-behind with ADO.NET Entity Framework:

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

You need to set the property ContentTemplate, not Content.

Try:

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

See this article

Mark Pim
excellent, that works, do you know how I can use StringFormat in this context? http://stackoverflow.com/questions/577697/how-can-i-get-multibinding-to-work-in-xaml-listbox
Edward Tanguay