gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 19:40:27 25 4
gpt4 key购买 nike

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

<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 属性也不起作用。

编辑
当我将其更改为下方时,它会在按钮周围留下填充/边距。
我已将 Margin 和 Padding 的 Setter 设置为 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>

enter image description here

编辑2
<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您在模板内提供对象以接收 Background 的属性.没有它,您的 Setter 将一无所有实际设置。这就是背景不起作用的原因,因为那里没有任何东西可以接受该属性。

但是,您不一定需要 Border来完成它。您也可以直接使用 Background="{TemplateBinding Background}"并将其直接应用于您的StackPanel因为它还有一个 Background无论如何属性(property)可用。

您的填充和边距问题是一回事。您在那里没有实际接受您指定的 Setter 的地方。所以你想要填充和边距,你要么需要离开你的边框并为这些属性添加 TemplateBindings,要么 StackPanel至少支持 Margin所以你可以跨越两者并通过做创建“填充”;
<StackPanel Margin="{TemplateBinding Padding}"..../>
除非您的背景颜色周围有空间,因为背景位于现在也有边距的对象上。有道理?与默认的 Button 模板进行比较,并注意缺少的内容。基本上这里的经验法则是。如果你想在实际控制级别设置它,如 <Button Background="blah" Margin="Blah"..../>那么模板中的某些内容需要可用于您尝试使用的声明。至少当你还在学习模板是如何工作的时候。希望这可以帮助。

附录;

好吧,既然我们发现了Button实际上不是你的问题,而是 parent 的问题。试试这个。

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

关于wpf - 删除 ListViewItem 周围的空间填充边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36014405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com