简体   繁体   English

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

[英]Remove the space padding margin around a ListViewItem

Would like to add style to the button, and don't understand why I have to include this line of code, where I don't want to add any Border to my button: 想要为按钮添加样式,并且不明白为什么我必须包含这行代码,我不想在我的按钮中添加任何边框:

<Border Background="{TemplateBinding Background}">

The complete code: 完整的代码:

<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>

I would keep this like that but there are some other padding or margin issues which I can't resolved too. 我会保持这样,但还有一些其他填充或边距问题,我也无法解决。

When I don't have this border, Setter Property for Background color also doesn't work. 当我没有此边框时,背景颜色的Setter属性也不起作用。

EDIT When I change it to below, it leave me with the padding/margin around the button. 编辑当我将其更改为下面时,它会留下按钮周围的填充/边距。 I have set Setters for Margin and Padding to 0, but this doesn't work. 我已将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 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>

So by having that TemplateBinding you're providing THE object inside the template to receive that property of Background . 因此,通过使用TemplateBinding您可以在模板中提供THE对象以接收Background属性。 Without it, there's nothing for your Setter to actually set. 没有它,你的Setter实际上没有任何设置。 Which is why Background doesn't work, because there's nothing in there accepting that property. 这就是为什么背景不起作用的原因,因为那里没有接受该属性的东西。

However you don't necessarily need the Border to accomplish it. 但是,您不一定需要Border才能完成它。 You could also just take that Background="{TemplateBinding Background}" and apply it directly to your StackPanel since it also has a Background property available anyway. 您也可以使用Background="{TemplateBinding Background}"并将其直接应用于StackPanel因为它还具有可用的Background属性。

Your padding and margin issues are the same thing. 你的填充和边距问题是一回事。 You have no where in there to actually accept the Setters you're specifying. 你没有在那里真正接受你指定的Setters。 So you want padding and margin you'll either need to leave your Border and add TemplateBindings for those properties, or StackPanel atleast supports Margin so you could cross the two and create "Padding" by doing; 因此,您需要填充和边距,您需要离开边框并为这些属性添加TemplateBindings,或者StackPanel至少支持Margin这样您就可以跨越两者并通过执行创建“填充”;

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

Except then your background color will have space around it since Background is on the object that now also has margin. 除此之外,您的背景颜色将在其周围留出空间,因为背景位于现在也具有边距的对象上。 Make sense? 合理? Compare to a default Button template and notice what's missing. 与默认的Button模板比较,注意缺少的内容。 Basically the rule of thumb here is. 基本上这里的经验法则是。 If you want to set it at the actual control level like <Button Background="blah" Margin="Blah"..../> then something inside the template needs to be available for that declaration you're trying to use. 如果你想把它设置在实际控制级别,比如<Button Background="blah" Margin="Blah"..../>那么模板里面的东西需要用于你想要使用的声明。 At least while you're still learning how templating works anyway. 至少在你还在学习模板是如何工作的时候。 Hope this helps. 希望这可以帮助。

Addendum; 附录;

Ok so since we figured out Button isn't actually your issue but the parent. 好吧,因为我们发现Button实际上不是你的问题,而是父母。 Try this instead. 试试这个。

<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