2016-08-22 32 views
1

如何在Windows UWP中为分段控制创建自定义的渲染器。我跟着这个link自定义渲染在IOS和Android的分段控制。但我找不到任何Windows UWP。任何人都可以提供任何资源,或者是否有任何其他方式做到这一点,类似于Windows UWP中的分段控制。Xamarin:用于Windows的分段控制uwp

enter image description here

回答

2

我最近发现了nuget包FreshEssentials。它具有分段按钮组的实现。按照这个github link找到实现。它很容易使用。

+0

Asesome @Himanshu(y)! –

+0

还有这个:(https://github.com/1iveowl/Plugin.SegmentedControl –

1

我自定义风格的单选按钮做了一次。为您的按钮编辑RadioButton样式 - 创建正在查看的已选中和未选中样式,并为其分配相同的组,以便当时只能选择一个按钮。 不知道有没有更简单的方法。

编辑:SAMPE代码,编辑您想要的方式:我找到了解决自己

<Page.Resources> 
    <Style x:Key="RadioButtonStyle1" TargetType="RadioButton"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="Foreground" Value="{ThemeResource RadioButtonContentForegroundThemeBrush}"/> 
     <Setter Property="Padding" Value="0,0,0,0"/> 
     <Setter Property="HorizontalAlignment" Value="Center"/> 
     <Setter Property="VerticalAlignment" Value="Stretch"/> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/> 
     <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="RadioButton"> 
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
         Background="{TemplateBinding Background}"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="PointerOver"> 

           </VisualState> 
           <VisualState x:Name="Pressed"> 

           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundRectangle"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource RadioButtonDisabledBackgroundThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource RadioButtonContentDisabledForegroundThemeBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="CheckStates"> 
           <VisualState x:Name="Checked"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundRectangle"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Blue"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="White"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Unchecked"> 

           </VisualState> 
           <VisualState x:Name="Indeterminate"/> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"> 
            <Storyboard> 
             <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualWhite"/> 
             <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualBlack"/> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Unfocused"/> 
           <VisualState x:Name="PointerFocused"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 


         <Grid HorizontalAlignment="Stretch"> 
          <Grid x:Name="BackgroundRectangle" Background="Transparent"> 
           <Border BorderThickness="1" BorderBrush="White" Margin="0" HorizontalAlignment="Stretch"> 
            <Rectangle Margin="0" StrokeThickness="{ThemeResource RadioButtonBorderThemeThickness}" HorizontalAlignment="Stretch" UseLayoutRounding="False"/> 
           </Border> 
           <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="Title"></TextBlock> 

          </Grid> 
          <ContentPresenter x:Name="ContentPresenter" Foreground="White" AutomationProperties.AccessibilityView="Raw" 
              ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" 
              Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
              Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Grid> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Page.Resources> 


<Grid Height="40" Background="Black" BorderThickness="0"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="10"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="10"/> 
    </Grid.ColumnDefinitions> 

    <RadioButton Height="30" Content="Button1" Grid.Column="1" Style="{StaticResource RadioButtonStyle1}" GroupName="MainGroup" Checked="Button1_OnChecked"></RadioButton> 
    <RadioButton Height="30" Content="Button2" Grid.Column="2" Style="{StaticResource RadioButtonStyle1}" GroupName="MainGroup" Checked="Button2_OnChecked"></RadioButton> 
    <RadioButton Height="30" Content="Button3" Grid.Column="3" Style="{StaticResource RadioButtonStyle1}" GroupName="MainGroup" Checked="Button3_OnChecked"></RadioButton> 
    <RadioButton Height="30" Content="Button4" Grid.Column="4" Style="{StaticResource RadioButtonStyle1}" GroupName="MainGroup" Checked="Button4_OnChecked"></RadioButton> 
</Grid> 
+0

能否请您对UWP定制提供一些代码。是的,它可以用单选按钮来完成,但不确定在这种情况下uwp中使用的布局模板。 –

+0

在样式开始的行下添加了样本代码 – RTDev

+0

的编辑,您可以看到一些行 - rg。 设置填充0,然后您可以使用此值与任何控制定义在使用Padding =“{TemplateBinding Padding}”的样式,关于ThemeResource您可以在这里阅读:https ://msdn.microsoft.com/en-us/windows/uwp/xaml-platform/themeresource-markup-extension,但你会propably只需要ThemeResource采用的信息使用默认的系统风格 - 所以你可以改变(而且应该)每个值=“{ThemeResource ...}”值=“红色”或任何其他你想要的值 – RTDev