1

I have a project that needs to target .NET Framework 3.5 and 4.5, and I want to set property of a WPF control according to the build target. E.g . I have a textblock, and I want its background to be Azure if the build target is 3.5, Cyan if the build target is 4.5 How can I do that?

<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>

Tung
  • 25
  • 3
  • Hi, welcome to SO. As far as my understanding goes you might not be able to do this in XAML alone. You would need to check the version in code behind and bind it to the TextBlock. In that case the Style defined might not be required as well. If this is ok with you please update someone might be able to help. – Arif Eqbal Oct 29 '19 at 06:26

2 Answers2

0

It's possible to do that with an Interaction.Behaviors you can set this property in XAML included in a style. Inside the behavior you can read the framework version. Here is an example.

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

You can use this code to read the framework version inside the behavior

    string version = Assembly.GetExecutingAssembly().GetReferencedAssemblies().Where(c => c.Name.Contains("mscorlib")).FirstOrDefault().Version.ToString();

Hope this helps.

Yannick Turbang
  • 378
  • 4
  • 8
0

You could use the Environment.Version that returns the CLR version. The idea here is to define a DataTrigger in your xaml bound to a boolean, true if the version starts with 4, false otherwise (.net 3.5 has a CLR version that starts with 2), take a look at the CLR versions here.

Your xaml should look something like that:

<....
        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>

With a property defined in the VM/code behind:

public bool IsAbove4 { get; set; } = Environment.Version.ToString().StartsWith("4");
SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47