How do I say .ToString() in XAML?

白昼怎懂夜的黑 提交于 2019-12-11 08:59:23

问题


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:


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



来源:https://stackoverflow.com/questions/577608/how-do-i-say-tostring-in-xaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!