I have the following XAML Code at the bottom, which correpsonds to the numbertextbox highlighted in the picture: My Goal is to have the current year as the defualt number in this numbertextbox when the radio button 'Current' is selected. It is currently defaulted to 0 when the radio button 'Current is Selected'. Can this be done via the XAML in the view or would the change occur in the view-model?
<tools:NumberTextBox x:Name="txtYear" FocusManager.FocusedElement="{Binding ElementName=txtYear}" Width="100" Text="{Binding Path=HistoryYear, UpdateSourceTrigger=PropertyChanged}"/>
You could define the current year in the XAML statically like this
where "s:" is defined as System (shown below), giving you access to objects in the System namespace.
HOWEVER: You should not do this for a couple reasons. The main one is that you cannot, to my knowledge, dynamically get the current year using something like DateTime.Now in Window.Resources, because the value stored where "2020" is in the example above must be a string. This would require manual updating every year, etc.
What you should do, is bind the Text property of the TextBox to a property in a ViewModel, or set it in the code-behind if you aren't using view models in this project yet. This can be achieved by creating a bindable property in a view model like this
ViewModel
View (the xaml)
Alternatively, you could do this in the code-behind like this
View.xaml
View.xaml.cs (code-behind)
Final Thoughts: If you want to only display the current year when "current" is selected, you would just update the value of
CurrentYear
based on the value of the RadioButtons, etc. This would be done in the ViewModel or code-behind depending on your app's architecture.