Search code examples
c#uwpcolor-picker

C# UWP fix ColorPicker's appearance


In C# uwp since i have been added XamlControlsResources to my app.xaml, my ColorPicker's appearance on the UI totally changed. I have fixed to most of it but i didn't find anything how can i change the inner selector's size in the ColorSpectrum.

enter image description here

Any idea to reduce this selector size?


Solution

  • Add a new style for ColorSpectrum and use this new style in ColorPickerStyle.

    You can find the styles of TimePicker in generic.xaml which is available in the \(Program Files)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.xxxxx.0\Generic.

     <Grid x:Name="SelectionEllipsePanel" Width="16" Height="16">
    

    The default size of inner selector is 16*16, and you can change its Width and Height.

    <Page.Resources>
    
         <Style x:Key="ColorSpectrumNewStyle" TargetType="ColorSpectrum">
            <Setter Property="ManipulationMode" Value="None" />
            <Setter Property="UseSystemFocusVisuals" Value="False" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ColorSpectrum">
                        <Grid x:Name="LayoutRoot">
    
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal" />
    
                                    <VisualState x:Name="PointerOver">
                                        <VisualState.Setters>
                                            <Setter Target="SelectionEllipse.Opacity" Value="0.8" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="Pressed" />
                                    <VisualState x:Name="PressedLarge">
                                        <VisualState.Setters>
                                            <Setter Target="SelectionEllipsePanel.Width" Value="48" />
                                            <Setter Target="SelectionEllipsePanel.Height" Value="48" />
                                        </VisualState.Setters>
                                    </VisualState>
    
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="SelectionEllipseColor">
                                    <VisualState x:Name="SelectionEllipseLight" />
                                    <VisualState x:Name="SelectionEllipseDark">
                                        <VisualState.Setters>
                                            <Setter Target="FocusEllipse.Stroke" Value="{ThemeResource SystemControlBackgroundChromeWhiteBrush}" />
                                            <Setter Target="SelectionEllipse.Stroke" Value="{ThemeResource SystemControlBackgroundChromeBlackHighBrush}" />
                                        </VisualState.Setters>
                                    </VisualState>
    
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="ShapeSelected">
                                    <VisualState x:Name="BoxSelected" />
                                    <VisualState x:Name="RingSelected">
                                        <VisualState.Setters>
                                            <Setter Target="SpectrumRectangle.Visibility" Value="Collapsed" />
                                            <Setter Target="SpectrumOverlayRectangle.Visibility" Value="Collapsed" />
                                            <Setter Target="RectangleBorder.Visibility" Value="Collapsed" />
                                            <Setter Target="SpectrumEllipse.Visibility" Value="Visible" />
                                            <Setter Target="SpectrumOverlayEllipse.Visibility" Value="Visible" />
                                            <Setter Target="EllipseBorder.Visibility" Value="Visible" />
                                        </VisualState.Setters>
                                    </VisualState>
    
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Unfocused" />
                                    <VisualState x:Name="Focused">
                                        <VisualState.Setters>
                                            <Setter Target="FocusEllipse.Visibility" Value="Visible" />
                                        </VisualState.Setters>
                                    </VisualState>
                                    <VisualState x:Name="PointerFocused" />
    
                                </VisualStateGroup>
    
                            </VisualStateManager.VisualStateGroups>
                            <Grid x:Name="SizingGrid" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <Grid.Clip>
                                    <RectangleGeometry />
                                </Grid.Clip>
                                <Rectangle x:Name="SpectrumRectangle" IsHitTestVisible="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                                <Rectangle x:Name="SpectrumOverlayRectangle" IsHitTestVisible="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                                <Ellipse x:Name="SpectrumEllipse" IsHitTestVisible="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Collapsed" Margin="0,0,-1,-1" />
                                <Ellipse x:Name="SpectrumOverlayEllipse"   IsHitTestVisible="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Collapsed" Margin="0,0,-1,-1" />
                                <Canvas x:Name="InputTarget" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Control.IsTemplateFocusTarget="True">
                                    <!--modify here-->
                                    <Grid x:Name="SelectionEllipsePanel" Width="50" Height="50">
                                        <Ellipse x:Name="FocusEllipse" Stroke="{ThemeResource SystemControlBackgroundChromeBlackHighBrush}" Margin="-2" StrokeThickness="2" IsHitTestVisible="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Visibility="Collapsed" />
                                        <Ellipse x:Name="SelectionEllipse"  Stroke="{ThemeResource SystemControlBackgroundChromeWhiteBrush}" StrokeThickness="2" IsHitTestVisible="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                            <ToolTipService.ToolTip>
                                                <ToolTip x:Name="ColorNameToolTip" VerticalOffset="20" />
                                            </ToolTipService.ToolTip>
                                        </Ellipse>
    
                                    </Grid>
                                </Canvas>
                                <Rectangle x:Name="RectangleBorder" Style="{StaticResource ColorPickerBorderStyle}" IsHitTestVisible="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                                <Ellipse x:Name="EllipseBorder"  Style="{StaticResource ColorPickerBorderStyle}" IsHitTestVisible="False" Visibility="Collapsed" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="-0.5,-0.5,-1.5,-1.5" />
    
                            </Grid>
    
                        </Grid>
    
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <Style x:Key="ColorPickerBorderStyle" TargetType="Shape">
            <Setter Property="Stroke" Value="{ThemeResource SystemControlForegroundListLowBrush}"/>
            <Setter Property="StrokeThickness" Value="2"/>
        </Style>
        <Style x:Key="ColorPickerStyle1" TargetType="ColorPicker">
            <Setter Property="MaxWidth" Value="392"/>
            <Setter Property="MinWidth" Value="312"/>
            <Setter Property="IsTabStop" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ColorPicker">
                        <Grid Background="{TemplateBinding Background}">
                            ...
                            ...
                            <StackPanel>
                                <Grid x:Name="ColorSpectrumGrid">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <!--add ColorSpectrumNewStyle here-->
                                    <ColorSpectrum x:Name="ColorSpectrum" Style="{StaticResource ColorSpectrumNewStyle}" Grid.Column="0" Components="{TemplateBinding ColorSpectrumComponents}" MinHue="{TemplateBinding MinHue}" MaxWidth="336" MaxSaturation="{TemplateBinding MaxSaturation}" MinSaturation="{TemplateBinding MinSaturation}" MinWidth="256" MaxHeight="336" MinHeight="256" MinValue="{TemplateBinding MinValue}" MaxValue="{TemplateBinding MaxValue}" MaxHue="{TemplateBinding MaxHue}" Grid.Row="0" Shape="{TemplateBinding ColorSpectrumShape}"/>
                                    <Grid x:Name="ColorPreviewRectangleGrid" Grid.Column="1" Margin="12,0,0,0" Grid.Row="0" Width="44">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition/>
                                            <ColumnDefinition/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition/>
                                            <RowDefinition/>
                                        </Grid.RowDefinitions>
                                        <Rectangle Grid.ColumnSpan="2" Grid.RowSpan="2" VerticalAlignment="Stretch">
                                            <Rectangle.Fill>
                                                <ImageBrush x:Name="ColorPreviewRectangleCheckeredBackgroundImageBrush"/>
                                            </Rectangle.Fill>
                                        </Rectangle>
                                        <Rectangle x:Name="ColorPreviewRectangle" Grid.ColumnSpan="2" Grid.RowSpan="2" VerticalAlignment="Stretch"/>
                                        <Rectangle x:Name="PreviousColorRectangle" Grid.ColumnSpan="2" Grid.Row="1" VerticalAlignment="Stretch" Visibility="Collapsed"/>
                                        <Rectangle x:Name="BorderRectangle" Grid.ColumnSpan="2" Grid.RowSpan="2" Style="{StaticResource ColorPickerBorderStyle}"/>
                                    </Grid>
                                </Grid>
      
                                  ...
                                  ...
                            </StackPanel>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>
    
    <Grid>
        <ColorPicker Style="{StaticResource ColorPickerStyle1}">
            
        </ColorPicker>
    </Grid>
    

    enter image description here