不同的Windows XP样式正在改变我的控件外观,我该如何避免它?

时间:2010-09-03 16:34:43

标签: .net wpf xaml

我认为我的用户控件,样式和布局等一切都很好看,直到我发布了一个供用户测试的版本。他们询问切换按钮在检查时是否为绿色。我说是的,但事实并非如此。我检查了我的机器,它是绿色的。结果他有一个不同于我的Windows XP风格。也就是说,他有'Windows经典风格'。

无论Windows风格如何,我如何避免这种情况并强制执行我的风格?

<UserControl.Resources>
    <Style x:Key="MyToggStyle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Content" Value="On" />
        <Setter Property="IsChecked" Value="True" />
        <Setter Property="Background" Value="Green" />
        <Style.Triggers>
            <Trigger Property="IsChecked" Value="False">
                <Setter Property="Content" Value="Pff" />
                <Setter Property="Background" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
    <ToggleButton FontWeight="Bold" 
                  IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"
                  Style="{StaticResource MyToggStyle}"/>
</Grid>

2 个答案:

答案 0 :(得分:2)

您必须完全覆盖控件模板,因为默认的chrome会使用操作系统设置。

在Expression Blend中比在Visual Studio中更容易做到。

以下是ToggleButton的默认模板的简化版:

<Style TargetType="{x:Type ToggleButton}">
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding Button.IsDefaulted}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                        </Microsoft_Windows_Themes:ButtonChrome>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#ADADAD"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

请注意,该控件使用Microsoft_Windows_Themes:ButtonChrome。如果你摆脱或替换了那个chrome,你的用户显示应该与你自己的匹配(不要删除ContentPresenter - 这是按钮文本/内容的去向)。如果你只是删除它,你将有一个平面按钮。您可以为它创建视觉状态和动画,但同样在Blend中更容易。

注意:在这种情况下,Microsoft_Windows_Themes别名的命名空间是  xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"

答案 1 :(得分:1)

使用第三方UI控件将帮助您保持一致性。

我是使用DevExpress控件完成的。它们具有非常好的蒙皮功能。

我个人不喜欢不尊重操作系统外观设置的应用程序。

不要忘记使用标准的.NET UI控件可以让您利用新的操作系统功能。例如,在Windows XP,Windows Vista和Windows 7上尝试使用标准ProgressBar控件的应用程序。