質問

リストボックスで項目を選択すると、リストボックス項目の周囲にある種の境界線またはスペースが表示され、画像に表示される線が作成されます。丸で囲んだ線を削除したいです。

すでにあります HorizontalContentAlignment = "Stretch" ところで。

私の何が間違っているのでしょうか?

問題:

alt text

役に立ちましたか?

解決

マットの言う通り、それは一種のバグです。パディングは、変更可能なプロパティに関連付けられるのではなく、ListBox テンプレート内でハードコーディングされます。

これを修正する最善の方法は、次のようにテンプレートを再定義することです。

<ListBox>
    <ListBox.Template>
        <ControlTemplate TargetType="{x:Type ListBox}">
            <Border 
                SnapsToDevicePixels="true" 
                x:Name="Bd" 
                Background="{TemplateBinding Background}" 
                BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}" 
                Padding="0"> <!-- This was originally 1 -->
                <ScrollViewer 
                    Focusable="false" 
                    Padding="{TemplateBinding Padding}">
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </ScrollViewer>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter 
                        Property="Background" 
                        TargetName="Bd" 
                        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                </Trigger>
                <Trigger Property="IsGrouping" Value="true">
                    <Setter 
                        Property="ScrollViewer.CanContentScroll" 
                        Value="false"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ListBox.Template>

    <ListBoxItem>Item1</ListBoxItem>
    <ListBoxItem>Item2</ListBoxItem>
    <ListBoxItem>Item3</ListBoxItem>
</ListBox>

他のヒント

ListBoxのバグだと思います。ここでは、ListBox の幅を満たすように項目テンプレートを拡張する方法を示します。各リスト項目のコンテナーを 1 セルのグリッドにし、グリッドをロードしたイベントを使用して、グリッドを ListBox の幅まで拡張するメソッドを呼び出します。

<ListBox x:Name="lvHolePatterns"  ItemsSource="{Binding HolePatterns}"
                              SelectedItem="{Binding ActivePattern, Mode=TwoWay}"
                              HorizontalContentAlignment="Stretch"
                              ScrollViewer.VerticalScrollBarVisibility="Visible"
                              Background="Gray">

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid Loaded="StretchFrameworkElement">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Border Margin="0,3,5,3" BorderThickness="1" BorderBrush="SlateGray" CornerRadius="4"
                RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Stretch" >
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF000000" Offset=" 1"/>
                        <GradientStop Color="#FF396DBE" Offset="0"/>
                    </LinearGradientBrush>
                </Border.Background>
                <StackPanel Orientation="Vertical" >
                    <TextBlock FontWeight="Bold" Text="{Binding Path=PatternName}" Foreground="WHITE" VerticalAlignment="Center"  Margin="5,5,0,5"/>
                    <StackPanel Orientation="Horizontal" Margin="5,0,0,5">
                        <TextBlock Text="{Binding Path=HoleCount}" Margin="10,0,0,0" Foreground="WHITE" VerticalAlignment="Center"/>
                        <TextBlock Text=" Holes" Margin="3,0,0,0" Foreground="WHITE" VerticalAlignment="Center"/>
                        <CheckBox Content="Visible" IsChecked="{Binding Visible, Mode=TwoWay}" Foreground="WHITE" Margin="10,0,0,0" />
                    </StackPanel>
                </StackPanel>
            </Border>
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

次に、グリッドロードイベントを処理するために次のメソッドを追加します。

private void StretchFrameworkElement(object sender, RoutedEventArgs e)
{
    // found this method at: http://silverlight.net/forums/p/18918/70469.aspx#70469
    FrameworkElement t = sender as FrameworkElement;
    ContentPresenter p = VisualTreeHelper.GetParent(t) as ContentPresenter;
    p.HorizontalAlignment = HorizontalAlignment.Stretch;       
}

これは、私のアプリの WPF バージョンからの ListView です。ListView DataTemplate は Border を使用し、ListView の幅に合わせて自動的に拡張されます。上記の Silverlight の回答のように、追加のメソッドは必要ありません。

<ListView x:Name="lvHolePatterns" ItemsSource="{Binding Path=HolePatterns}"
                              SelectedItem="{Binding Path=ActivePattern}"
                              IsSynchronizedWithCurrentItem="True"
                              HorizontalContentAlignment="Stretch"
                              ScrollViewer.VerticalScrollBarVisibility="Visible"
                              Background="Gray">
    <ListView.ItemContainerStyle>
        <Style BasedOn="{StaticResource lvRowHighlighter}" TargetType="{x:Type ListViewItem}">
            <!--<Setter Property="Height" Value="50" />-->
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Margin="0,3,5,3" BorderThickness="1" BorderBrush="SlateGray" CornerRadius="4" RenderTransformOrigin="0.5,0.5">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF000000" Offset=" 1"/>
                        <GradientStop Color="#FF396DBE" Offset="0"/>
                    </LinearGradientBrush>
                </Border.Background>
                <StackPanel Orientation="Vertical">
                    <TextBlock FontWeight="Bold" Text="{Binding Path=PatternName}" Foreground="WHITE" VerticalAlignment="Center"  Margin="5,5,0,5"/>
                    <StackPanel Orientation="Horizontal" Margin="5,0,0,5">
                        <TextBlock Text="{Binding Path=HoleCount}" Margin="15,0,0,0" Foreground="WHITE" VerticalAlignment="Center"/>
                        <TextBlock Text=" Holes" Margin="3,0,0,0" Foreground="WHITE" VerticalAlignment="Center"/>
                        <CheckBox Content="Visible" IsChecked="{Binding Path=Visible}" Foreground="WHITE" Margin="10,0,0,0" />
                </StackPanel>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

リストボックス項目のハードコーディングされたパディング問題に対する非常に簡単な回避策を見つけました。datatemplate の最も外側の要素のマージンを -1 に設定します。

-- ヘノン

これをテキスト ボックスのインスタンス属性に追加するだけです。

HorizontalContentAlignment="Stretch"

これは、背後にコードがなくても機能します。

ギリ

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top