How to set XAML Element to default to current year?

395 views Asked by At

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?

enter image description here

enter image description here

<tools:NumberTextBox x:Name="txtYear" FocusManager.FocusedElement="{Binding ElementName=txtYear}" Width="100" Text="{Binding Path=HistoryYear, UpdateSourceTrigger=PropertyChanged}"/>
2

There are 2 answers

0
ClicheCoffeeMug On BEST ANSWER

You could define the current year in the XAML statically like this

<Window.Resources>
    <s:DateTime x:Key="CurrentYear">2020</s:DateTime>
</Window.Resources>

where "s:" is defined as System (shown below), giving you access to objects in the System namespace.

xmlns:s="clr-namespace:System;assembly=mscorlib"

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

public string CurrentYear { get; set; }

public MainViewModel()
{
    // This could be set anywhere, but setting in the constructor like this works well for a default value.
    CurrentYear = DateTime.Now.Year.ToString();
}

View (the xaml)

<!-- TwoWay Binding because the text can be updated by the user in the view, and it could be updated by the ViewModel -->
<TextBox Text="{Binding CurrentYear, 
                        Mode=TwoWay, 
                        UpdateSourceTrigger=PropertyChanged}" />

Alternatively, you could do this in the code-behind like this

View.xaml

<TextBox x:Name="currentYearTextBox" />

View.xaml.cs (code-behind)

public MainWindow()
{
    InitializeComponent();
    currentYearTextBox.Text = DateTime.Now.Year.ToString();
}

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.

1
Prajeesh T S On

You can use the following code

<Window.Resources>
        <sys:String x:Key="YearStyle">yyyy</sys:String>
    </Window.Resources>

<TextBlock Text="{Binding 
           Source={x:Static sys:DateTime.Now},StringFormat={StaticResource YearStyle}}"/>

where "sys:" is defined as System (shown below) namespace.

xmlns:sys="clr-namespace:System;assembly=mscorlib"