Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据目标设置WPF控件的样式?_Wpf_Properties_Multitargeting - Fatal编程技术网

如何根据目标设置WPF控件的样式?

如何根据目标设置WPF控件的样式?,wpf,properties,multitargeting,Wpf,Properties,Multitargeting,我有一个项目需要以.NETFramework3.5和4.5为目标,我想根据构建目标设置WPF控件的属性。例如。我有一个文本块,如果构建目标是3.5,我希望它的背景是Azure,如果构建目标是4.5,我希望它的背景是青色,我怎么能做到这一点 <Window x:Class="WpfAppMultipleTarget.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml

我有一个项目需要以.NETFramework3.5和4.5为目标,我想根据构建目标设置WPF控件的属性。例如。我有一个文本块,如果构建目标是3.5,我希望它的背景是Azure,如果构建目标是4.5,我希望它的背景是青色,我怎么能做到这一点

<Window x:Class="WpfAppMultipleTarget.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfAppMultipleTarget"
    mc:Ignorable="d"
    Title="MainWindow" Height="300" Width="300">
<Grid>
    <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Background" Value="Azure"/> <!-- If target is net framework 3.5 -->
            <Setter Property="Background" Value="Cyan"/> <!-- If target is net framework 4.5 -->
        </Style>
    </Grid.Resources>
    <TextBlock>Hello</TextBlock>
</Grid>

你好

可以通过交互来实现。您可以在样式中包含的XAML中设置此属性。 在行为中,您可以阅读框架版本。 这里有一个例子

<Style TargetType="{x:Type TextBox}">
  <Style.Setters>
    <Setter Property="i:Interaction.Behaviors">
      <Setter.Value>
          <local:BehaviorName/>
          <local:BehaviorName/>
      </Setter.Value>
    </Setter>
  </Style.Setters>
</Style>

希望这有帮助。

通过交互可以做到这一点。您可以在样式中包含的XAML中设置此属性。 在行为中,您可以阅读框架版本。 这里有一个例子

<Style TargetType="{x:Type TextBox}">
  <Style.Setters>
    <Setter Property="i:Interaction.Behaviors">
      <Setter.Value>
          <local:BehaviorName/>
          <local:BehaviorName/>
      </Setter.Value>
    </Setter>
  </Style.Setters>
</Style>
希望这有帮助。

您可以使用返回CLR版本的。这里的想法是在xaml中定义一个绑定到布尔值的
DataTrigger
,如果版本以4开头,则为true,否则为false(.net 3.5的CLR版本以2开头),请查看CLR版本

您的xaml应该如下所示:

<....
        Title="MainWindow" Height="450" Width="800" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
</Window.Resources>

<Grid>
    <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsAbove4}" Value="True" >
                    <Setter Property="Background" Value="Cyan"/>
                </DataTrigger>
            </Style.Triggers>
            <Setter Property="Background" Value="Azure"/>
        </Style>
    </Grid.Resources>
    <TextBlock>Hello</TextBlock>
</Grid>
您可以使用返回CLR版本的。这里的想法是在xaml中定义一个绑定到布尔值的
DataTrigger
,如果版本以4开头,则为true,否则为false(.net 3.5的CLR版本以2开头),请查看CLR版本

您的xaml应该如下所示:

<....
        Title="MainWindow" Height="450" Width="800" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
</Window.Resources>

<Grid>
    <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsAbove4}" Value="True" >
                    <Setter Property="Background" Value="Cyan"/>
                </DataTrigger>
            </Style.Triggers>
            <Setter Property="Background" Value="Azure"/>
        </Style>
    </Grid.Resources>
    <TextBlock>Hello</TextBlock>
</Grid>

嗨,欢迎来到苏。就我的理解而言,您可能无法单独在XAML中实现这一点。您需要在代码隐藏中检查版本并将其绑定到TextBlock。在这种情况下,可能也不需要定义的样式。如果这对你来说没问题,请更新可能有人能够提供帮助。嗨,欢迎来到SO。就我的理解而言,您可能无法单独在XAML中实现这一点。您需要在代码隐藏中检查版本并将其绑定到TextBlock。在这种情况下,可能也不需要定义的样式。如果您同意,请更新,可能有人可以帮助您。谢谢。我想你的建议也会有帮助。谢谢。我想你的建议也会有帮助。