“边距”或“宽度”属性会影响.Net Core中的WPF滑块样式 [英] Margin or Width property affects the WPF slider style in .Net Core
本文介绍了“边距”或“宽度”属性会影响.Net Core中的WPF滑块样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图覆盖System.Windows.Controls.Slider的默认模板。
我为拇指设置边距和宽度
< Thumb x:Name =" Thumb"宽度= QUOT; 5英寸Margin =" 0,2,-5,3" />
它适用于WPF。
在.Net核心中,它与拇指和减少按钮之间存在差距。
请在下面找到完整的代码段,
< Window.Resources>
< Style x:Key =" DecreaseButton" TargetType =" {x:Type RepeatButton}">
< Setter Property =" OverridesDefaultStyle"值= QUOT;真" />
< Setter Property =" Background"值= QUOT;透明" />
< Setter Property =" Focusable"值= QUOT假QUOT; />
< Setter Property =" IsTabStop"值= QUOT假QUOT; />
< Setter Property =" Template">
< Setter.Value>
< ControlTemplate TargetType =" {x:Type RepeatButton}">
< Border Background =" {TemplateBinding Background}"高度="{TemplateBinding Height}"宽度=" {TemplateBinding Width}">
< Rectangle Fill =" Black"高度= QUOT; 4.0"宽度=" {TemplateBinding Width}" />
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< Style x:Key =" IncreaseButton" TargetType =" {x:Type RepeatButton}">
< Setter Property =" OverridesDefaultStyle"值= QUOT;真" />
< Setter Property =" Background"值= QUOT;透明" />
< Setter Property =" Focusable"值= QUOT假QUOT; />
< Setter Property =" IsTabStop"值= QUOT假QUOT; />
< Setter Property =" Template">
< Setter.Value>
< ControlTemplate TargetType =" {x:Type RepeatButton}">
< Border Background =" {TemplateBinding Background}"高度="{TemplateBinding Height}"宽度=" {TemplateBinding Width}">
< Rectangle Fill ="#FFE5E5E5"高度= QUOT; 4.0"宽度=" {TemplateBinding Width}" />
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< ControlTemplate x:Key =" SliderThumbHorizontalDefault" TargetType =" {x:Type Thumb}">
< Grid HorizontalAlignment =" Center" VerticalAlignment = QUOT;中心">
< Path x:Name =" grip"数据="M 0,0 C0,0 11,0 11,0 11,0 11,18 11,18 11,18 0,18 0,18 0,18 0,0 0,0 z"填充= QUOT;黑色"伸展= QUOT;填写" SnapsToDevicePixels = QUOT;真"行程="黑色" StrokeThickness = QUOT 1 QUOT; VerticalAlignment = QUOT;中心" />
< / Grid>
< / ControlTemplate>
< ControlTemplate x:Key =" SliderHorizontal" TargetType =" {x:Type Slider}">
< Border x:Name =" border" BorderBrush =" {TemplateBinding BorderBrush}" BorderThickness =" {TemplateBinding BorderThickness}" Background =" {TemplateBinding Background}" SnapsToDevicePixels = QUOT;真">
< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =" Auto" />
< RowDefinition Height =" Auto" MinHeight =" {TemplateBinding MinHeight}" />
< RowDefinition Height =" Auto" />
< /Grid.RowDefinitions>
< TickBar x:Name =" TopTick" Fill =" {TemplateBinding Foreground}"高度= QUOT; 4英寸余量= QUOT; 0,0,0,2"放置= QUOT;陀螺" Grid.Row = QUOT; 0"能见度= QUOT;折叠" />
< TickBar x:Name =" BottomTick" Fill =" {TemplateBinding Foreground}"高度= QUOT; 4英寸余量= QUOT; 0,2,0,0"放置= QUOT;底座" Grid.Row = QUOT; 2英寸能见度= QUOT;折叠" />
< Border x:Name =" TrackBackground"了borderThickness = QUOT 1 QUOT;高度= QUOT; 4.0"余量= QUOT; 5,0" Grid.Row = QUOT 1 QUOT; VerticalAlignment = QUOT;中心">
< Canvas Margin =" -6,-1">
< Rectangle x:Name =" PART_SelectionRange" Fill =" {DynamicResource {x:Static SystemColors.HighlightBrushKey}}"高度= QUOT; 4.0"能见度= QUOT;隐藏" />
< / Canvas>
< / Border>
< Track x:Name =" PART_Track" Grid.Row = QUOT 1 QUOT;>
< Track.DecreaseRepeatButton>
< RepeatButton Command =" {x:Static Slider.DecreaseLarge}" Style =" {StaticResource DecreaseButton}"余量= QUOT; 0,0,&-0.333,0 QUOT; />
< /Track.DecreaseRepeatButton>
< Track.IncreaseRepeatButton>
< RepeatButton Command =" {x:Static Slider.IncreaseLarge}" Style =" {StaticResource IncreaseButton}" />
< /Track.IncreaseRepeatButton>
< Track.Thumb>
< Thumb x:Name =" Thumb"可聚焦= QUOT假QUOT;高度= QUOT; 15" OverridesDefaultStyle = QUOT;真" Template =" {StaticResource SliderThumbHorizontalDefault}" VerticalAlignment = QUOT;中心"宽度= QUOT; 5英寸余量= QUOT; 0,2,-5,3"的HorizontalAlignment = QUOT;右" />
< /Track.Thumb>
< / Track>
< / Grid>
< / Border>
< / ControlTemplate>
< Style x:Key =" SliderStyle" TargetType =" {x:Type Slider}">
< Setter Property =" Stylus.IsPressAndHoldEnabled"值= QUOT假QUOT; />
< Setter Property =" Background"值= QUOT;透明" />
< Setter Property =" BorderBrush"值= QUOT;透明" />
< Setter Property =" Template"值=" {StaticResource SliderHorizontal}" />
< / Style>
< /Window.Resources>
< Grid>
< Slider Width =" 300"值= QUOT; 4英寸高度= QUOT; 20" Style =" {StaticResource SliderStyle}" >< /滑块>
< / Grid>
此问题可以解决吗?
$
解决方案
嗨 &NBSP; DeepakG211,
>>在.Net核心中它带有差距拇指和减少按钮之间。 (由于我是MSDN论坛的新手,我无法附上图片供参考)
我试过WPF应用。这是工作。
可能是由Net Core设计引起的。目前,我们没有办法解决这个问题。
b
据我所知,
.NET Core 3和对Windows桌面应用程序的支持。 .NET Core 3.0.0预览用于测试改进。请耐心等待正式版。
此外,当您对NET Core有疑问时,请访问NET Core论坛以获取合适的内容。帮助。
最好的问候,
Yong路
I am trying to override the default template of System.Windows.Controls.Slider.
Where I set a margin and width for the thumb
<Thumb x:Name="Thumb" Width="5" Margin="0,2,-5,3"/>
It comes properly in WPF.
While in .Net core it comes with a gap between thumb and decrease button.
Please find the complete code snippet below,
<Window.Resources> <Style x:Key="DecreaseButton" TargetType="{x:Type RepeatButton}"> <Setter Property="OverridesDefaultStyle" Value="true"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="Focusable" Value="false"/> <Setter Property="IsTabStop" Value="false"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type RepeatButton}"> <Border Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"> <Rectangle Fill="Black" Height="4.0" Width="{TemplateBinding Width}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="IncreaseButton" TargetType="{x:Type RepeatButton}"> <Setter Property="OverridesDefaultStyle" Value="true"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="Focusable" Value="false"/> <Setter Property="IsTabStop" Value="false"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type RepeatButton}"> <Border Background="{TemplateBinding Background}" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}"> <Rectangle Fill="#FFE5E5E5" Height="4.0" Width="{TemplateBinding Width}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <ControlTemplate x:Key="SliderThumbHorizontalDefault" TargetType="{x:Type Thumb}"> <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Path x:Name="grip" Data="M 0,0 C0,0 11,0 11,0 11,0 11,18 11,18 11,18 0,18 0,18 0,18 0,0 0,0 z" Fill="Black" Stretch="Fill" SnapsToDevicePixels="True" Stroke="Black" StrokeThickness="1" VerticalAlignment="Center"/> </Grid> </ControlTemplate> <ControlTemplate x:Key="SliderHorizontal" TargetType="{x:Type Slider}"> <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <TickBar x:Name="TopTick" Fill="{TemplateBinding Foreground}" Height="4" Margin="0,0,0,2" Placement="Top" Grid.Row="0" Visibility="Collapsed"/> <TickBar x:Name="BottomTick" Fill="{TemplateBinding Foreground}" Height="4" Margin="0,2,0,0" Placement="Bottom" Grid.Row="2" Visibility="Collapsed"/> <Border x:Name="TrackBackground" BorderThickness="1" Height="4.0" Margin="5,0" Grid.Row="1" VerticalAlignment="center"> <Canvas Margin="-6,-1"> <Rectangle x:Name="PART_SelectionRange" Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" Height="4.0" Visibility="Hidden"/> </Canvas> </Border> <Track x:Name="PART_Track" Grid.Row="1"> <Track.DecreaseRepeatButton> <RepeatButton Command="{x:Static Slider.DecreaseLarge}" Style="{StaticResource DecreaseButton}" Margin="0,0,-0.333,0"/> </Track.DecreaseRepeatButton> <Track.IncreaseRepeatButton> <RepeatButton Command="{x:Static Slider.IncreaseLarge}" Style="{StaticResource IncreaseButton}"/> </Track.IncreaseRepeatButton> <Track.Thumb> <Thumb x:Name="Thumb" Focusable="False" Height="15" OverridesDefaultStyle="True" Template="{StaticResource SliderThumbHorizontalDefault}" VerticalAlignment="Center" Width="5" Margin="0,2,-5,3" HorizontalAlignment="Right"/> </Track.Thumb> </Track> </Grid> </Border> </ControlTemplate> <Style x:Key="SliderStyle" TargetType="{x:Type Slider}"> <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="Template" Value="{StaticResource SliderHorizontal}"/> </Style> </Window.Resources> <Grid> <Slider Width="300" Value="4" Height="20" Style="{StaticResource SliderStyle}" ></Slider> </Grid>
Can this issue be resolved?
解决方案
Hi DeepakG211,
>>While in .Net core it comes with a gap between thumb and decrease button. (As I am new to MSDN forum, I cannot attach the images for reference)
I have tried on WPF application. It is work.
May be it cause by the Net Core design. Currently, We have no solution to solve the issue.
As far as I know, .NET Core 3 and Support for Windows Desktop Applications. The NET Core 3.0.0-preview are being used for test improvement. Please be patient and wait for the official version.
Besides, when you have a question about NET Core, please go to the NET Core forum for getting suitable help.
Best Regards,
Yong Lu
这篇关于“边距”或“宽度”属性会影响.Net Core中的WPF滑块样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文