繁体   English   中英

删除ListViewItem周围的空间填充边距

[英]Remove the space padding margin around a ListViewItem

想要为按钮添加样式,并且不明白为什么我必须包含这行代码,我不想在我的按钮中添加任何边框:

<Border Background="{TemplateBinding Background}">

完整的代码:

<Style x:Key="ButtonStyleRed" TargetType="{x:Type Button}">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}">
                    <StackPanel Orientation="Horizontal" Width="200">
                        <Rectangle Width="4" Height="30" Fill="#64dd17" Margin="0,0,10,1" RadiusX="2" RadiusY="2"/>
                        <TextBlock Text="{Binding Path=DataContext.FlowStageName,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}" 
                                   VerticalAlignment="Center" FontSize="14" Foreground="White" TextWrapping="WrapWithOverflow"/>
                    </StackPanel>
                 </Border>
                 <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True"/>
                    <Trigger Property="IsDefaulted" Value="True"/>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#263238"></Setter>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True"/>
                    <Trigger Property="IsEnabled" Value="False"/>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Margin" Value="0"/>
    <Setter Property="Padding" Value="0"></Setter>
    <Setter Property="Width" Value="200"/>
    <Setter Property="Height" Value="50"/>
    <Setter Property="Background" Value="#37474f"/>
    <Setter Property="BorderThickness" Value="0"/>
</Style>

我会保持这样,但还有一些其他填充或边距问题,我也无法解决。

当我没有此边框时,背景颜色的Setter属性也不起作用。

编辑当我将其更改为下面时,它会留下按钮周围的填充/边距。 我已将Setters for Margin和Padding设置为0,但这不起作用。

<StackPanel Orientation="Horizontal" Width="200" Background="{TemplateBinding Background}" Margin="{TemplateBinding Padding}">
                      <Rectangle Width="4" Height="30" Fill="#64dd17" Margin="0,0,10,1" RadiusX="2" RadiusY="2"/>
                      <TextBlock Text="{Binding Path=DataContext.FlowStageName,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}}" 
                                   VerticalAlignment="Center" FontSize="14" Foreground="White" TextWrapping="WrapWithOverflow"/>
                 </StackPanel>

在此输入图像描述

EDIT2

<views:BaseView.Resources>
    <views:SwapBooleanValueConverter x:Key="SwapBooleanValueConverter" />
    <DataTemplate x:Key="FlowStagesTemplate">
        <StackPanel>
            <Button x:Name="MenuStageButton"
                    Tag="{Binding ID}"
                    Command="{Binding DataContext.OnButtonClickCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
                    CommandParameter="{Binding ElementName=TurulStageButton}"
                    Style="{Binding FlowStageDisplayStyle}">
            </Button>
            <Rectangle VerticalAlignment="Stretch" Width="200" Margin="0" Height="1">
                <Rectangle.Fill>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="1,0" >
                        <GradientStop Color="#263238" Offset="0" />
                        <GradientStop Color="#78909c" Offset="0.5" />
                        <GradientStop Color="#263238" Offset="1.0" />
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
        </StackPanel>
    </DataTemplate>
</views:BaseView.Resources>
<StackPanel Background="#263238">
    <ListView ItemsSource="{Binding FlowStagesSubMenu}" ItemTemplate="{StaticResource FlowStagesTemplate}" 
              BorderThickness="0" Background="#263238" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
</StackPanel>

因此,通过使用TemplateBinding您可以在模板中提供THE对象以接收Background属性。 没有它,你的Setter实际上没有任何设置。 这就是为什么背景不起作用的原因,因为那里没有接受该属性的东西。

但是,您不一定需要Border才能完成它。 您也可以使用Background="{TemplateBinding Background}"并将其直接应用于StackPanel因为它还具有可用的Background属性。

你的填充和边距问题是一回事。 你没有在那里真正接受你指定的Setters。 因此,您需要填充和边距,您需要离开边框并为这些属性添加TemplateBindings,或者StackPanel至少支持Margin这样您就可以跨越两者并通过执行创建“填充”;

<StackPanel Margin="{TemplateBinding Padding}"..../>

除此之外,您的背景颜色将在其周围留出空间,因为背景位于现在也具有边距的对象上。 合理? 与默认的Button模板比较,注意缺少的内容。 基本上这里的经验法则是。 如果你想把它设置在实际控制级别,比如<Button Background="blah" Margin="Blah"..../>那么模板里面的东西需要用于你想要使用的声明。 至少在你还在学习模板是如何工作的时候。 希望这可以帮助。

附录;

好吧,因为我们发现Button实际上不是你的问题,而是父母。 试试这个。

<ListView ItemsSource="{Binding FlowStagesSubMenu}" 
          ItemTemplate="{StaticResource FlowStagesTemplate}" 
          BorderThickness="0" Background="#263238"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
   <ListView.ItemContainerStyle>
      <Style TargetType="ListViewItem">
         <Setter Property="Padding" Value="0"/>
         <Setter Property="Margin" Value="0"/>
         <Setter Property="BorderThickness" Value="0"/>
      </Style>
   </ListView.ItemContainerStyle>
   <ListView.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel Orientation="Vertical"/>
      </ItemsPanelTemplate>
   </ListView.ItemsPanel>
</ListView>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM