Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: DataTemplate and CurrentCulture

$
0
0
Hi again,

I'm using the property grid with DataTemplates. Now I'm having a problem with the language / culture. I have the impression that the propertygrid is ignoring it when using data templates.

My current culture is de-DE. The language is set at startup, should be inherited. In the app.xaml.cs, I have added this:
    protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement),
            new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
        }
When you start the application, you'll see a normal textbox that is bound to Blubber.ValidationDouble2 (which is a double). In this textbox I can type some stuff and have immediate validation. I can enter the double number with my locale decimal separator (which is a comma in german culture). It will translate the input, update the binding correctly and display the double value correctly formatted with a comma. Everything fine with that (just to demonstrate that my WPF settings and culture settings are working).

When I do the same without a data template, the property grid behaves like the text box above. I have correct behaviour in the grid. Text input is accepted with comma, correctly converted to the double, and it will be formatted with a comma. Everything correct. I would love to use it, but I want to have immediate validation while typing. That means I need to use the data template (to be able to specify UpdateSourceTrigger=PropertyChanged).

When I do it with a data template I have a very strange behavior: the double value is formatted in my locale - the field displays the double value correctly using the comma. But when I ENTER some values in the field, it ignores the comma (it obviously expects input to use a dot) and converts the string to double by ignoring my decimal separator. The result itself is then is formatted for displaying using the current culture (with comma)! That means it accepts input only with dot and for formatting it is using a comma. Very weird.

For example I have:
1,1234 initially when the grid is loaded. I press backspace, then I see 1123,00. When I press backspace again, I'll see 11230,00.

Obviously it first deletes the "4" and converts 1,123 to 1123. This is the problem, it ignores my current culture when converting "1,123" to double. As a result it just ignores the "," and the result is 1123 which is formatted (using my CurrentCulture settings) to 1123,0
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:lcl="clr-namespace:WpfApplication1"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tk="http://schemas.xceed.com/wpf/xaml/toolkit"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525"
        >
  
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <!--<TextBox              Text="{Binding Value,                     UpdateSourceTrigger=PropertyChanged, StringFormat='{}{0:F2}'}"></TextBox>-->
        <TextBox Grid.Row="0" Text="{Binding Blubber.ValidationDouble2, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, ConverterCulture=de-DE}">
            </TextBox>
            <tk:PropertyGrid   Grid.Row="1 " Margin="5"  IsEnabled="True" ScrollViewer.VerticalScrollBarVisibility="Visible" Name="propertyGrid" 
                       MinWidth="400" UpdateTextBoxSourceOnEnterKey="True" HorizontalContentAlignment="Left" NameColumnWidth="250" ClipToBounds="False" 
                       VerticalContentAlignment="Stretch" Height="Auto" MaxHeight="800" DockPanel.Dock="Top" ShowSearchBox="False" ShowSortOptions="False" ShowSummary="False" ShowTitle="False"
                        SelectedObject="{Binding Blubber, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" ShowAdvancedOptions="False" IsCategorized="True" AutoGenerateProperties="True">
  
            <tk:PropertyGrid.EditorDefinitions>
                
                <tk:EditorTemplateDefinition >
                    <tk:EditorTemplateDefinition.TargetProperties>
                        <tk:TargetPropertyType Type="{x:Type s:String}" />
                    </tk:EditorTemplateDefinition.TargetProperties>
                    <tk:EditorTemplateDefinition.EditingTemplate>
                        <DataTemplate>
                            <tk:PropertyGridEditorTextBox TextAlignment="Left" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </tk:EditorTemplateDefinition.EditingTemplate>
                </tk:EditorTemplateDefinition>

                <tk:EditorTemplateDefinition >
                    <tk:EditorTemplateDefinition.TargetProperties>
                        <tk:TargetPropertyType Type="{x:Type s:Int32}" />
                        <tk:TargetPropertyType Type="{x:Type s:Int16}" />
                        <tk:TargetPropertyType Type="{x:Type s:Int64}" />
                    </tk:EditorTemplateDefinition.TargetProperties>
                    <tk:EditorTemplateDefinition.EditingTemplate>
                        <DataTemplate>
                            <tk:PropertyGridEditorIntegerUpDown TextAlignment="Left" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </tk:EditorTemplateDefinition.EditingTemplate>
                </tk:EditorTemplateDefinition>
               <!-- <tk:EditorTemplateDefinition >
                    <tk:EditorTemplateDefinition.TargetProperties>
                        <tk:TargetPropertyType Type="{x:Type s:Single}" />
                    </tk:EditorTemplateDefinition.TargetProperties>
                    <tk:EditorTemplateDefinition.EditingTemplate>
                        <DataTemplate>
                            <tk:PropertyGridEditorSingleUpDown TextAlignment="Left" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </tk:EditorTemplateDefinition.EditingTemplate>
                </tk:EditorTemplateDefinition>-->
                <tk:EditorTemplateDefinition >
                    <tk:EditorTemplateDefinition.TargetProperties>
                        <tk:TargetPropertyType Type="{x:Type s:Double}" />
                    </tk:EditorTemplateDefinition.TargetProperties>
                    <tk:EditorTemplateDefinition.EditingTemplate>
                        <DataTemplate>
                            
                            <StackPanel>
                                <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True}"></TextBox>
                            </StackPanel>
                        </DataTemplate>
                    </tk:EditorTemplateDefinition.EditingTemplate>
                </tk:EditorTemplateDefinition>
            </tk:PropertyGrid.EditorDefinitions>
        </tk:PropertyGrid>
        <TextBox 
            Grid.Row="2" Text="{Binding Blubber.ValidationTest, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        
        
    </Grid>
   
</Window>
I would be glad if anyone had a solution to this issue...

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>