【问题标题】:How do I say .ToString() in XAML?XAML 中的 .ToString() 怎么说?
【发布时间】:2009-02-23 13:24:45
【问题描述】:

以下代码给了我错误(无法将类型 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;

【问题讨论】:

    标签: xaml tostring


    【解决方案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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-04
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 2017-04-11
    • 2011-11-29
    相关资源
    最近更新 更多