4

If I have a TextBlock in the corner of my UserControl is it possible to bind the Text property to my Assembly Version Number Which is in AssemblyInfo.cs

WPF:

<TextBlock Text="{Binding AssemblyVersion}"/>

AssemblyInfo.cs

[assembly: AssemblyVersion("1.0.0.0")]
JKennedy
  • 18,150
  • 17
  • 114
  • 198

3 Answers3

7

Create a readonly property named AssemblyVersion and bind it.

public Version AssemblyVersion
{
    get
    {
        return Assembly.GetEntryAssembly().GetName().Version;
    }
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Great answer. Is there much difference between the way to get the Version in this method and the one used [Here](http://stackoverflow.com/questions/6493715/how-to-get-current-product-version-in-c)? apart from your way being much simpler – JKennedy Aug 15 '14 at 12:02
  • @user1 Yes, [This](http://stackoverflow.com/a/6499302/2530848) adds support for application deployed via clickonce. Otherwise, my answer is fine – Sriram Sakthivel Aug 15 '14 at 12:06
4

here is a pure XAML approach

<TextBlock xmlns:ref="clr-namespace:System.Reflection;assembly=mscorlib">
    <TextBlock.Text>
        <Binding Path="Version">
            <Binding.Source>
                <ObjectDataProvider MethodName="GetName">
                    <ObjectDataProvider.ObjectInstance>
                        <ObjectDataProvider MethodName="GetExecutingAssembly"
                                            ObjectType="{x:Type ref:Assembly}" />
                    </ObjectDataProvider.ObjectInstance>
                </ObjectDataProvider>
            </Binding.Source>
        </Binding>
    </TextBlock.Text>
</TextBlock>

in this example we are leveraging the ObjectDataProvider to retrieve the desired (executing or can say current) assembly followed by it's version.

ObjectDataProvider is quite useful for retrieving results from method calls.

pushpraj
  • 13,458
  • 3
  • 33
  • 50
0

To get the assembly version you can use this in code behind:

using System.Reflection;
using System.Diagnostics;

    #region - Version -
    /// <summary>
    /// Get the FileVersion
    /// </summary>
    public static string Version => FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
    #endregion - Version -

and in the Xaml code:

<TextBlock Text="{Binding Version}"/>

that's all

luka
  • 605
  • 8
  • 15