Pergunta

O código a seguir dá-me o erro (não pode adicionar o tipo de objeto para StackPanel).

Como posso dizer .ToString () em 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>

obrigatório em code-behind com ADO.NET Entity Framework:

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

Solução

Você precisa definir o ContentTemplate propriedade, não Content.

Tente:

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

este artigo

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top