Home > database >  WPF adding version in text
WPF adding version in text

Time:11-22

why this is not working?

XAML:

<TextBlock Text="{Binding Path=Ver}"/>

CODE:

public partial class MainWindow : Window
{
    public Version Ver=> Assembly.GetExecutingAssembly().GetName().Version;
    public MainWindow()
    {
        InitializeComponent();
    }
}

why is this not working? do i need another dll?

CodePudding user response:

To make the binding work you have to set DataContext property of the TextBlock element itself or the parent element, e.g. the MainWindow:

public partial class MainWindow : Window
{
    public Version Ver=> Assembly.GetExecutingAssembly().GetName().Version;
    public MainWindow()
    {
        DataContext = this;
        InitializeComponent();
    }
}

Note that this is a minimal working example. It's better to follow MVVM pattern and create a separate INotifyPropertyChanged implementation class with Ver property and set the DataContext to the implementation instance instead of the window itself.

  • Related