سؤال

يعطيني الكود التالي الخطأ (لا يمكن إضافة كائن اكتب إلى StackPanel).

كيف يمكنني أن أقول .tostring () في 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>

ملزم في رمز - خلف مع إطار كيان ADO.NET:

MainEntities db = new MainEntities();
var customers = from c in db.CustomersSet
                select c;
theCustomers.ItemsSource = customers;
هل كانت مفيدة؟

المحلول

تحتاج إلى تعيين الخاصية 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>

يرى هذه المقالة

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top