Reputation: 81
I have this datepicker element:
<DatePicker
Name="DataSelected"
Grid.Column="1" Grid.Row="2"
SelectedDate="{Binding DataSelected, Mode=TwoWay}"
CalendarOpened="DatePicker_Opened">
<DatePicker.Resources>
<Style TargetType="DatePickerTextBox">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox"
Text="{Binding Path=SelectedDate,
RelativeSource={RelativeSource AncestorType={x:Type DatePicker}},
StringFormat={x:Static local:MyView.DateFormat}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
Now based on the value of the DateFormat variable which can be "yyyy" or "MM-yyyy" or "dd-MM-yyyy" I want to change the format of the datepicker. Because I want the user to able to select/see only the year if DateFormat is 'yyyy' or year and month if the variable is "MM-yyyy" and so on. How can I do that? (My code is in C#) (I am sorry for any grammatical errors, English is not my first language)
Upvotes: -1
Views: 62
Reputation: 414
As discussed in the comments here is the code I used to change the StringFormat "dynamically" for a textbox. Where DecimalPlaces is an integer that I used to trigger the changes
<TextBox>
<TextBox.Style>
<Style TargetType="TextBox" BasedOn="{StaticResource TextBoxTemplate}">
<Setter Property="Text" Value="{Binding CurrentPosition, StringFormat=F3}"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding DecimalPlaces}" Value="0">
<Setter Property="Text" Value="{Binding CurrentPosition , StringFormat=F0}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding DecimalPlaces}" Value="1">
<Setter Property="Text" Value="{Binding CurrentPosition, StringFormat=F1}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding DecimalPlaces}" Value="2">
<Setter Property="Text" Value="{Binding CurrentPosition, StringFormat=F2}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Upvotes: 0