10

Windows Phone 8 アプリで、ListBoxItemの幅全体にListBox.

私のListBox

<ListBox 
      ItemsSource="{Binding Events}" 
      behaviors:ItemClickCommandBehavior.Command="{Binding EventSelectedCommand}"
      ItemTemplate="{StaticResource EventListTemplateSelector}"/>

その DataTemplates は別の xaml リソース ファイルにあります。

<DataTemplate x:Key="EventListHeaderTemplate">
    <Border HorizontalAlignment="Stretch">
        <Grid Height="50">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="6*"/>
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Source="{Binding ImageUri}" VerticalAlignment="Center" HorizontalAlignment="Center" Height="30"/>
            <TextBlock Grid.Column="1" Text="{Binding SomeText}" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="Black"/>
        </Grid>
    </Border>
</DataTemplate>

アイテムを実際に伸ばすことができず、問題がどこにあるのかわかりません。Horizo​​ntalCONtentAlignment="Stretch"を設定しようとしましたが、ItemContainerStyleうまくいきませんでした。私は他の多くの組み合わせを試しましたが、Border または Grid 幅を定数に設定するだけで機能するようです。他の 1 つの解決策は、Border 幅を設定して、含まれている ListBox の ActualWidth にバインドすることですが、Stretch を使用したいバリアントなら、それを機能させることができます。

4

1 に答える 1

27

私は XAML で同じ問題にTextBlock遭遇し、幅全体が完全に色付けされていない理由を不思議に思っていました。

競合するスタイルを操作する方法 (これは実際にはどの xaml バリアントでも機能しListBoxItemます) は、スペースの使用を処理するためにスタイルを明示的に定義することです。

これにより、次のように画面領域に入力 ( Stretch ) する必要があるというヒントが xaml に与えられます。

    <ListBox Name="lbTest" HorizontalContentAlignment="Stretch"  >

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment"
                        Value="Stretch"/>
            </Style>
        </ListBox.ItemContainerStyle>

        <ListBox.ItemTemplate>...</ListBox.ItemTemplate>

それ以外の場合、デフォルトでは、xaml パーサーListBoxItem. その恐ろしいスコッチ テープの外観を与えます。

于 2013-06-06T15:57:52.403 に答える