How to specify using the default style in XAML?

Richard Haggard 6 Reputation points
2020-06-30T19:18:10.64+00:00

The basic problem is that a WPF application I'm working on makes extensive use of Material Design. The problem I'm running into is that it appears that the DatePicker style makes it very difficult to apply a converter to get its output to look like "01/23/2020" instead of "01-23-2020". I can do this in a non-Material Designs environment but the exact same code refuses to call the converter that reformats the DateTime string to the format I want. To test this hypothesis I thought that I'd use a standard DatePicker instead of the Mater Designs restyled version but I'm having trouble figuring out how to tell XAML to not use Material Design's restyled DatePicker. How can that be done?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. gekka 9,666 Reputation points MVP
    2020-06-30T22:39:34.017+00:00

    Set x:null as the style to be the default style.

    If you are using MaterialDesignThemes, you can overwrite the resource for the key named 'MaterialDesignDatePickerTextBox'.

    <StackPanel>
        <StackPanel.Resources>
            <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
        </StackPanel.Resources>
    
        <Border  Margin="10">
    
            <DatePicker Style="{x:Null}"><!-- Clear Style -->
                <DatePicker.Resources>
                    <Style TargetType="{x:Type DatePickerTextBox}">
                        <Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DatePicker},Path=SelectedDate,StringFormat=dd/MM/yyyy}" />
                    </Style>
                </DatePicker.Resources>
            </DatePicker>
        </Border>
    
    
        <Border BorderBrush="Black" BorderThickness="1" Margin="10">
            <DatePicker >
                <DatePicker.Resources>
    
                    <!-- Inherit style of MaterialDesign -->
                    <Style TargetType="{x:Type DatePickerTextBox}" 
                            x:Key="MaterialDesignDatePickerTextBox" 
                            BasedOn="{StaticResource MaterialDesignDatePickerTextBox}">
                        <Setter Property="Text" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DatePicker},Path=SelectedDate,StringFormat=dd/MM/yyyy}" />
                    </Style>
    
                </DatePicker.Resources>
            </DatePicker>
        </Border>
    </StackPanel>
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.