【问题标题】:CheckBox tick mirrored when changing FlowDirection更改 FlowDirection 时镜像复选框勾选
【发布时间】:2014-08-04 17:08:43
【问题描述】:

操作系统: 微软视窗 8.1 开发应用程序:Microsoft Visual Studio 2013(WPF 应用程序开发)

我已经从 Windows 7 迁移到 Windows 8.1,现在我在 VS2012 中开发的旧应用程序有一种奇怪的方式。因为我的语言是从右到左的,所以我将RightToLeft 用于FlowDirection。它工作正常,因为所有文本都显示在 CheckBox 的左侧,但现在 CheckBox 的勾号镜像如下:

即使在从右到左的语言中,勾号也不正确。这是因为 O.S.或VS2013,我该如何解决?我需要创建一个模板吗?谢谢。

【问题讨论】:

  • 通常情况并非如此,tick 保持正常。您是否使用任何第三方控件?你能发布使用的模板或控件(如果有的话)吗?还发布您如何使用 xaml 中的复选框。
  • 我不使用任何第三方控件。只是简单的复选框,再一次:它在 Windows 7 或 vs2012 上运行良好,因为在更改为 windows 8.1 和 vs2013 后它会这样做

标签: wpf


【解决方案1】:

我遇到了同样的问题,这是我找到的最佳解决方案。路径是绘制刻度的原因,因此只需将其流向更改回 LeftToRight 即可使其按预期绘制。

<CheckBox Content="My Checkbox" FlowDirection="RightToLeft">
    <CheckBox.Resources>
         <Style TargetType="{x:Type Path}">
               <Setter Property="FlowDirection" Value="LeftToRight"/>
         </Style>
    </CheckBox.Resources>
</CheckBox>

【讨论】:

    【解决方案2】:
    <Grid>
        <Grid.Resources>
            <Style x:Key="ArabicStyle" TargetType="{x:Type CheckBox}">
                <Style.Resources>
                    <Style TargetType="{x:Type Path}">
                        <Setter Property="FlowDirection" Value="LeftToRight"/>
                    </Style>
                </Style.Resources>
            </Style>
        </Grid.Resources>
        <CheckBox Content="My Checkbox:" Style="{StaticResource ArabicStyle}"/>
    </Grid>
    

    【讨论】:

    • 很好,甚至比公认的答案更好,因为我不必将它放在每个复选框上。出于某种原因,我从没想过将资源放在一种风格中。这对我来说是一个不错的新工具。
    【解决方案3】:

    当您想使用标准复选框控件时,将路径的 FlowDirection 设置为 LeftToRight 将起作用,但如果您创建自定义模板,它将不起作用,因此像这样为 CheckBox 制作自定义样式模板并将主网格的 FlowDirection 设置为 LeftToRight " :

         <Style TargetType="{x:Type CheckBox}">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="Height" Value="30" />
            <Setter Property="FocusVisualStyle" Value="{DynamicResource MyFocusVisualStyte}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <BulletDecorator>
                            <BulletDecorator.Bullet>
                                <!-- ****************** here set FlowDirection to LeftToRight *********************************** -->
                                <Grid      FlowDirection="LeftToRight" Height="{TemplateBinding Height}" Width="{Binding RelativeSource={RelativeSource Self}, Path=Height, UpdateSourceTrigger=PropertyChanged}"
                              MinHeight="30" MinWidth="30" ShowGridLines="False">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="4*" />
                                        <ColumnDefinition Width="1*" />
                                        <ColumnDefinition Width="1*" />
                                        <ColumnDefinition Width="4*" />
                                        <ColumnDefinition Width="1*" />
                                        <ColumnDefinition Width="1*" />
                                        <ColumnDefinition Width="2*" />
                                        <ColumnDefinition Width="2*" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="3*" />
                                        <RowDefinition Height="1*" />
                                        <RowDefinition Height="1*" />
                                        <RowDefinition Height="1*" />
                                        <RowDefinition Height="4*" />
                                        <RowDefinition Height="1*" />
                                        <RowDefinition Height="1*" />
                                        <RowDefinition Height="4*" />
                                    </Grid.RowDefinitions>
    
                                    <Border Name="MainBorder"
                                    Grid.ColumnSpan="9" Grid.RowSpan="9"
                                    CornerRadius="4"
                                    BorderThickness="1"
                                    Background="Transparent" />
    
                                    <Border Name="InnerBorder"
                                    Grid.Column="1" Grid.ColumnSpan="5"
                                    Grid.Row="2" Grid.RowSpan="5"
                                    BorderThickness="1"
                                    BorderBrush="#808080" />
    
                                    <Path Name="InnerPath"
    
                                  Grid.Column="1" Grid.ColumnSpan="5"
                                  Grid.Row="2" Grid.RowSpan="5"
                                  Data="M31,5 L19.5,5 19.5,19.5 34.5,19.5 34.5,11.75"
                                  Stretch="Fill" Stroke="Red"/>
    
                                    <Path Name="CheckMark"
                                  Grid.Column="2" Grid.ColumnSpan="5"
                                  Grid.Row="1" Grid.RowSpan="5"
                                  Opacity="0"
                                  Data="M9.07743946676476E-09,4.31805768640244L4.68740335877841,8.86361158398516C4.68740335877841,8.86361158398516,16.3281249985376,-2.42451336648723,16.3281249985376,-2.42451336648723L14.0622100581796,-4.77304938341948 4.68740335877846,4.31805791992662 2.22656251699567,1.93164208562579z"
                                  Fill="#3babe3"
                                  Stretch="Fill"
                                  Stroke="#3babe3" />
    
                                    <Path Name="InderminateMark"
                                  Grid.Column="3"
                                  Grid.Row="4"
                                  Data="M0,4 L1,5 5,1 4,0"
                                  Opacity="0"
                                  Stretch="Fill"
                                  StrokeThickness="0"
                                  Fill="#808080" />
                                </Grid>
                            </BulletDecorator.Bullet>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CheckStates">
                                    <VisualState x:Name="Checked">
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="CheckMark" Duration="0:0:0.2" To="1" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unchecked" >
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="CheckMark" Duration="0:0:0.2" To="0" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Indeterminate">
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="InderminateMark" Duration="0:0:0.2" To="1" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ContentPresenter Margin="4,0,4,0"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Left"
                        RecognizesAccessKey="True" />
                        </BulletDecorator>
    
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="InnerBorder" Property="Visibility" Value="Collapsed" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="MainBorder" Property="Background" Value="#81d2eb" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="CheckMark" Property="Fill" Value="#cccccc" />
                                <Setter TargetName="CheckMark" Property="Stroke" Value="#cccccc" />
                                <Setter TargetName="InnerPath" Property="Stroke" Value="#cccccc" />
                                <Setter TargetName="InderminateMark" Property="Fill" Value="#cccccc" />
                                <Setter TargetName="InnerBorder" Property="BorderBrush" Value="#cccccc" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <Style x:Key="MyFocusVisualStyte" >
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle x:Name="FocusStyle" StrokeDashArray="4 4" RadiusX="5" RadiusY="5" Fill="Transparent"
                           Stroke="#81d2eb" StrokeThickness="1" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 2018-10-03
      • 1970-01-01
      • 2010-12-15
      • 2014-07-21
      • 2013-12-30
      相关资源
      最近更新 更多