1

以下代码给了我错误(无法将类型 Object 添加到 Stackpanel)。

我如何在 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;
4

1 回答 1

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>

这篇文章

于 2009-02-23T13:37:23.373 回答