2

我有这样的风格:

<Style TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
               ...
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

默认情况下,它将应用于所有操作系统中的所有按钮,但我只想在用户使用 Windows 8 时应用它。

Environment.OSVersion.Version检查属性后,有什么方法可以从代码隐藏中激活样式?或者有没有更好的方法来做到这一点?

4

2 回答 2

1

我认为风格是基于主题的,不能基于操作系统版本。

于 2013-04-17T16:56:47.130 回答
1

找到您的引导程序代码(在显示 XAML 之前执行的代码)并创建简单的开关,该开关将根据操作系统版本选择正确的 XAML 文件。

Uri uri = new Uri("/OS7.xaml", UriKind.Relative);
StreamResourceInfo info = Application.GetResourceStream(uri);
XamlReader reader = new System.Windows.Markup.XamlReader();
var dic = (ResourceDictionary)reader.LoadAsync(info.Stream);

//then locate ResourceDictionary throgh Application.Current.Resources
yourDictionary.MergedDictionaries.Add(dic);

您还可以创建绑定到静态 OS 版本属性并切换 Button 模板的简单触发器,但它相当有限,因为您只能交换模板。它可能会帮助你;

   <Window.Resources>
                    <ControlTemplate x:Key="OS7" TargetType="Button">
                        <Border x:Name="Border" CornerRadius="2" BorderThickness="1" Background="blue" BorderBrush="blue">
                            <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>


        <!-- DEFALT -->
        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="Border" CornerRadius="2" BorderThickness="1" Background="red" BorderBrush="blue">
                            <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Style.Triggers>
                <!-- Just an example. Replace IsMouseOver with DataTrigger and STATIC binding against OS version-->
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Template" Value="{StaticResource OS7}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
于 2013-04-17T17:39:42.477 回答