locked
Globalize a Run Block in a Textblock RRS feed

  • Question

  • Hi,

    I am working with a Listview and a Datatemplate for that. In there I have two Textblock with Run Commands which should be globalized so I wanted to access the Run Blocks from the code behind. I havent found a solution for that, because I couldn't access the Run Blocks from Code Behind. Could anybody give me a hint how to solve that. The problem are the code blocks named "averageCompleteCars" and "averageLastRideCars". You could see that I already have a x:Uid defined but the two blocks should be now defined via code behind because I had to do some more checks. 

    Here is the datatemplate:

    <ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="0,10,0,0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <Grid>
                                        <TextBlock HorizontalAlignment="Left" Text="{Binding Name}" VerticalAlignment="Top" Style="{StaticResource BaseTextBlockStyle}" />
                                    </Grid>
                                    <Grid Grid.Row="1">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid Grid.Column="0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto" />
                                                <ColumnDefinition Width="Auto" />
                                            </Grid.ColumnDefinitions>
                                            <TextBlock x:Uid="odometerCarsList" Style="{StaticResource BaseTextBlockStyle}"/>
                                            <TextBlock Grid.Column="1" Text="{Binding Odometer}" Style="{StaticResource BaseTextBlockStyle}" Margin="5,0,0,0"/>
                                        </Grid>
                                        <TextBlock Grid.Column="1" Text="{Binding CreationTime, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:d}'}" Style="{StaticResource BaseTextBlockStyle}" FlowDirection="RightToLeft" />
                                    </Grid>
                                    <Grid Grid.Row="2">
                                        <TextBlock Style="{StaticResource BaseTextBlockStyle}" HorizontalAlignment="Left">
                                            <Run x:Uid="averageCompleteCars" />
                                            <Run Text="{Binding AverageComplete, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:f2}'}" />
                                        </TextBlock>
                                    </Grid>
                                    <Grid Grid.Row="3">
                                        <TextBlock Style="{StaticResource BaseTextBlockStyle}" HorizontalAlignment="Left">
                                            <Run x:Uid="averageLastRideCars" />
                                            <Run Text="{Binding AverageLastRide, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:f2}'}" />
                                        </TextBlock>
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>

    So is there a way to find the resource the Run Block and change it from code behind?

    Thanks,
    Michael


    Sunday, July 13, 2014 9:25 AM

Answers

  • The ItemTemplate is applied to all items in the ListView so you will have to access the two TextBlocks for each item in the ListView.

    If you want to make some changes to the bound value, e.g. localize it, you could use a converter:

    namespace App3
    {
        class MyConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                string name = value as string;
    
                //localize string....
    
                return "localized name...";
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    <ListView xmlns:local="clr-namespace:App3" ....>
    <ListView.Resources>
                    <local:MyConverter x:Key="converter"/>
    </ListView.Resources>
    ....
    <TextBlock HorizontalAlignment="Left" Text="{Binding Name, Converter={StaticResource converter}}" 
                               VerticalAlignment="Top" Style="{StaticResource BaseTextBlockStyle}" />
    
    Converters can be used to provide custom logic to a binding. Please refer to the following page for more information: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.ivalueconverter
    Sunday, July 13, 2014 11:39 AM

All replies

  • The ItemTemplate is applied to all items in the ListView so you will have to access the two TextBlocks for each item in the ListView.

    If you want to make some changes to the bound value, e.g. localize it, you could use a converter:

    namespace App3
    {
        class MyConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                string name = value as string;
    
                //localize string....
    
                return "localized name...";
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    <ListView xmlns:local="clr-namespace:App3" ....>
    <ListView.Resources>
                    <local:MyConverter x:Key="converter"/>
    </ListView.Resources>
    ....
    <TextBlock HorizontalAlignment="Left" Text="{Binding Name, Converter={StaticResource converter}}" 
                               VerticalAlignment="Top" Style="{StaticResource BaseTextBlockStyle}" />
    
    Converters can be used to provide custom logic to a binding. Please refer to the following page for more information: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.ivalueconverter
    Sunday, July 13, 2014 11:39 AM
  • if you definied a x:Uid  in your resource file simple add these keys:

    averageCompleteCars.Text and averageLastRideCars.Text from there you can add the translation. no need to access them from code behind


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, July 14, 2014 5:18 AM
  • Thanks, I will try that.

    Michael

    Wednesday, July 16, 2014 4:50 AM
  • Hi,

    I have this already but these are some settings which should be also overwritten via Code behind and should be changed with a Toggle-Button so thats the reason because I would change it the other way.

    Michael

    Wednesday, July 16, 2014 4:54 AM