gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:46:53 25 4
gpt4 key购买 nike

我认为我的用户控件、样式和布局等一切都很好,直到我发布了一个版本供用户测试。他们询问切换按钮是否可以在选中时为绿色。我说是,但不是。我检查了我的机器,它是绿色的。原来他的 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>

最佳答案

您必须完全覆盖控制模板,因为默认的 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"

关于.net - 不同的 Windows XP 样式正在改变我的控件外观,我该如何避免?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3637737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com