locked
DateTime windows metro c# RRS feed

  • Question

  • hello, 

    how to date format in application windows metro in textbox or textblock?

     <TextBox x:Name="txtDataIResult"
     Text="{Binding DataInicioRealiz,  StringFormat="dd/MM/yyyy"} />

    i try this, but not work

    thanks

    Monday, October 14, 2013 4:07 PM

Answers

  • Hi,Aurelien

    StringFormat is not available for windows store app. However you can use Binding.Converter property to format a string as you like it.

    According to your question, you can refer to some codes below:

    To use your converter in a binding, first create an instance of your converter class.  The following example shows this as a resource in a XAML file. And set the Converter property for the binding to that instance:

     <Page.Resources>
            <local:DateToStringConverter x:Key="datatimeConvert"/>
        </Page.Resources>
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="grid">
            <TextBox Margin="396,103,373,598" Text="{Binding datetime,Converter={StaticResource datatimeConvert},ConverterParameter='{}{0:dd/MM/yyyy}'}" ></TextBox>
        </Grid>

    In code-behind, define a converter class:

     public MainPage()
            {
                this.InitializeComponent();
                Timeconvert time=new Timeconvert{datetime=new DateTime(2008, 9, 23)};
                grid.DataContext = time;
               
            }
        }
    
        public class DateToStringConverter : IValueConverter
        {
    
            public object Convert(object value, Type targetType,
                object parameter, string language)
            {
                if (parameter == null)
                {
                    return value;
                }
    
                return String.Format((String)parameter, value);
            }
    
            public object ConvertBack(object value, Type targetType,
                object parameter, string language)
            {
                throw new NotImplementedException();
            }
    
        }
        public class Timeconvert
        {
            public DateTime datetime { get; set; }
        }

    For more information, you can see the link:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.binding.converter

    Best wishes!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Anne Jing Monday, October 21, 2013 1:05 AM
    Tuesday, October 15, 2013 3:08 AM
  • Hi Aurelien,

    here is a reusable StringFormat converter

    It allows you to format a date like this:

    <TextBlock Text="{Binding SomeDateTime, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd MMM yyyy}'}" />
    

    • Marked as answer by Anne Jing Monday, October 21, 2013 1:05 AM
    Tuesday, October 15, 2013 8:47 AM

All replies

  • make a property on your viewmodel that does do the formatting, or use a valueconverter

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, October 14, 2013 6:03 PM
  • thanks, and how to do it?
    Monday, October 14, 2013 8:57 PM
  • "make a property on your viewmodel that does do the formatting, or use a valueconverter"

    Unfortunately, those are the only options right now. Wish we could have an ETA for when someone will pull their head out of their ass and spend the FIVE MINUTES required to implement StringFormat on Bindings.

    Monday, October 14, 2013 10:44 PM
  • Hi,Aurelien

    StringFormat is not available for windows store app. However you can use Binding.Converter property to format a string as you like it.

    According to your question, you can refer to some codes below:

    To use your converter in a binding, first create an instance of your converter class.  The following example shows this as a resource in a XAML file. And set the Converter property for the binding to that instance:

     <Page.Resources>
            <local:DateToStringConverter x:Key="datatimeConvert"/>
        </Page.Resources>
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="grid">
            <TextBox Margin="396,103,373,598" Text="{Binding datetime,Converter={StaticResource datatimeConvert},ConverterParameter='{}{0:dd/MM/yyyy}'}" ></TextBox>
        </Grid>

    In code-behind, define a converter class:

     public MainPage()
            {
                this.InitializeComponent();
                Timeconvert time=new Timeconvert{datetime=new DateTime(2008, 9, 23)};
                grid.DataContext = time;
               
            }
        }
    
        public class DateToStringConverter : IValueConverter
        {
    
            public object Convert(object value, Type targetType,
                object parameter, string language)
            {
                if (parameter == null)
                {
                    return value;
                }
    
                return String.Format((String)parameter, value);
            }
    
            public object ConvertBack(object value, Type targetType,
                object parameter, string language)
            {
                throw new NotImplementedException();
            }
    
        }
        public class Timeconvert
        {
            public DateTime datetime { get; set; }
        }

    For more information, you can see the link:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.binding.converter

    Best wishes!



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Anne Jing Monday, October 21, 2013 1:05 AM
    Tuesday, October 15, 2013 3:08 AM
  • Hello Aurelien Mota,

    try to use programatically.

    For programatically :

     DateTime dt = DateTime.ParseExact("yourdatetime string",CultureInfo.InvariantCulture);
    Tuesday, October 15, 2013 4:39 AM
  • Hi Aurelien,

    here is a reusable StringFormat converter

    It allows you to format a date like this:

    <TextBlock Text="{Binding SomeDateTime, Converter={StaticResource StringFormatConverter}, ConverterParameter='{}{0:dd MMM yyyy}'}" />
    

    • Marked as answer by Anne Jing Monday, October 21, 2013 1:05 AM
    Tuesday, October 15, 2013 8:47 AM
  • hello Anne Jing,

    i use this, but i have a error here:

     <local:DateToStringConverter x:Key="datatimeConvert"/>
    "was not found"
     

    and the class DateToStringConverter is in code-behind

    thanks

    Tuesday, October 15, 2013 10:31 AM
  • Hello Aurelien Mota,

    For your class, use IValueConverter like,

    public sealed class BooleanToVisibilityConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                return value is Visibility && (Visibility)value == Visibility.Visible;
            }
        }

    try to clean & Build your project. May be helpful for you.

    Tuesday, October 15, 2013 10:44 AM