-1

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)

6
  • What is MyView.DateFormat? A POCO static property?
    – emoacht
    Commented Jul 4 at 12:39
  • @emoacht It is a public static string DateFormat = "MM-yyyy"; Commented Jul 4 at 12:44
  • I already had this kind of issue for decimal places in textblocks. What you can do is to put a datatrigger (or several) and depending on you trigger do again the binding of your textbox with another StringFormat
    – Alex R
    Commented Jul 4 at 12:48
  • @KristinaTamaro If so, what is your idea to notify the change of the string to the TextBox?
    – emoacht
    Commented Jul 4 at 12:51
  • @AlexR could you give me an example in written code of how you did it? Because I am not that familiar with WPF and am trying to learn. Thank you Commented Jul 4 at 12:53

1 Answer 1

0

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>
2
  • Thank you! In the code behind though how did you manage to change and give the new value to the textbox? Commented Jul 5 at 9:38
  • I do not use code behind, the value is in my ViewModel
    – Alex R
    Commented Jul 5 at 10:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.