Silverlight 3: ListBox DataTemplate HorizontalAlignment

Альтернативное решение предоставляется в np.intersect1d:

import numpy as np

array1 = [1,2,3,4,5,6]
array2 = [2,6,3,4,1,5]

np.intersect1d(array1, array2, return_indices=True)[2]
28
задан markti 6 October 2009 в 02:53
поделиться

3 ответа

When creating Data Templates for ListBox, you should not incldue . The contents of the DataTemplate will be placed inside of a generated container. You can control how that container is constructed using ItemContainerStyle.

The default control style for ListBoxItem is used to define the ItemContainerStyle by default. This style sets the ListBoxItem.HorizontalContentAlignment property to 'Left'. Notice how the ContentPresenter binds its HorizontalAlignment to this property.

You need to override the style of the ListBoxItem container that is being generated when you bind to your ListBox. This can be done by setting the ItemContainerStyle. Set the HorizontalContentAlignment property to be "Stretch".

Below is the default ListBoxItem Style. Included for reference.

<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
            <Setter Property="Padding" Value="3"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="TabNavigation" Value="Local"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid Background="{TemplateBinding Background}">
                            <!-- VSM excluded for readability -->
                            <Rectangle x:Name="fillColor" Fill="#FFBADDE9" RadiusX="1" RadiusY="1" IsHitTestVisible="False" Opacity="0"/>
                            <Rectangle x:Name="fillColor2" Fill="#FFBADDE9" RadiusX="1" RadiusY="1" IsHitTestVisible="False" Opacity="0"/>
                            <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                            <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1" StrokeThickness="1" RadiusX="1" RadiusY="1" Visibility="Collapsed"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
29
ответ дан Gabe 28 November 2019 в 02:21
поделиться

Здесь я заметил две вещи, потому что у меня была та же проблема, и я не мог ее решить так, как вы » повторная попытка.

Во-первых, вам не нужно явно помещать ListBoxItem в ваш DataTemplate. Он создается для вас автоматически, поэтому у вас есть элемент ListBoxItem внутри того, который был создан для вас. Я проверил это в Snoop для подтверждения.

Во-вторых, и я точно не знаю, почему, но я также не смог получить поведение растяжения из атрибутов выравнивания. Я изменил его, чтобы использовать привязку RelativeSource для атрибута Width к свойству ActualWidth содержащего ListBoxItem. Это сработало для меня.

Width="{Binding RelativeSource={RelativeSource 
   AncestorType={x:Type ListBoxItem}}, Path=ActualWidth}"

Если вам нужно установить свойства стиля в ListBoxItem, который неявно создан для вас, используйте элемент Style внутри ListBox.

-1
ответ дан Rich 28 November 2019 в 02:21
поделиться

Я потратил час, пытаясь решить эту проблему. Очень-очень расстраивает. Вы не должны отменять весь стиль по умолчанию для ListBoxItem. Я не мог заставить это работать. В конце концов, я решил проблему, просто переопределив только свойство HorizontalContentAlignment в моем разделе ListBox.ItemContainerStyle , например:

            <ListBox x:Name="ClassList" ItemsSource="{Binding LineClasses}"
                     ScrollViewer.VerticalScrollBarVisibility="Visible"
                     SelectionMode="Extended"
                     ScrollViewer.HorizontalScrollBarVisibility="Hidden" HorizontalContentAlignment="Stretch"
                     HorizontalAlignment="Stretch" Loaded="ClassList_Loaded"
                     VerticalAlignment="Stretch" Grid.Row="0">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
                    <ListBox.ItemTemplate>

                    <DataTemplate>
                        <Border BorderBrush="Black" CornerRadius="3" Background="#FFE88D34"
                            BorderThickness="1" HorizontalAlignment="Stretch" >
                            <Grid Background="Transparent" HorizontalAlignment="Stretch" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock 
                                    Grid.Column="0" HorizontalAlignment="Stretch"
                                    Margin="2"                                   
                                    FontSize="10"
                                    Text="{Binding DisplayClassNm}"/>
                            </Grid>

                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>

Это сработало для меня.

Майлз

68
ответ дан 28 November 2019 в 02:21
поделиться
Другие вопросы по тегам:

Похожие вопросы: