How to format a Date in windows phone 8? - xaml

I'm a little stuck trying to display a timespan data using the StringFormat property. I'm using the following code:
<TextBlock Text="{Binding Duration, StringFormat='Time: {0:HH.mm}'}" FontSize="12" />
When I run my App to test it, after to click on a button to retrieve some data (in which a timespan value is include), the App simply gets frozen.
However, If I don't use the string format property, the data is displayed OK in the format of:
00:03:49.4590000
The above output is the one I wanna displayed either as 00:03:49 (HH:mm:ss) or 03:49 (mm:ss)
Also, I found this code which suppously do the same:
<TextBlock Text="{Binding Duration, StringFormat=\{0\:N0\}}" FontSize="12" />
But, the resut is the same. The App gets frozen
Does anyone have a good example how to use the string format property to dislplay a TimeSpan value?
Or do I miss something the the above code?
Regards!

You could bind TimeSpan like this, textblock will display like 02:23(2 hours and 23 minutes):
<TextBlock Text="{Binding time, StringFormat='{}{0:hh\\:mm}'}"/>
also you can convert your TimeSpan to DateTime, and bind it like this:
<TextBlock Text="{Binding time, StringFormat='{}{0:HH:mm}'}"/>

Related

Is it possible to make the background-colour of a Canvas field dependent on its value?

I want one datetime field's background colour to depend on its value. Like if a certain date is passed, the background changes to red.
Is there a way to make this in XAML?
I know there is no possibility of an "if" condition/instruction, but maybe you guys found some way to implement a similar function.
<Canvas Canvas.Left="893" Canvas.Top="208" Height="25" Width="99" Background="red" Panel.ZIndex="-1"/>
<assembly:FieldControl Canvas.Left="890" Canvas.Top="206" FieldControlType="DateControl" FormField="{x:Null}" Height="25" LabelColumnWidth="0" Refnr="123456789" ShowCaption="False" StateImageAlignment="Hidden" Width="106" FontSize="10" Foreground="DimGray"/>
this is my code so far. The Canvas-Part makes the Background go red.
I also tried to put the background property in the "FieldControl" but there it's useless.
EDIT:
After getting the information, that Data Binding could help me with this problem, i tested it like this:
<TextBox Canvas.Left="890" Canvas.Top="226" Name="Date" Width="99" Height="25" VerticalAlignment="Top" Text="{Binding ElementName=Date, Path = SelectedItem.Content, Mode = TwoWay, UpdateSourceTrigger = PropertyChanged}" Background="{Binding ElementName=Date, Path=SelectedItem.Content}">
But this is not the direction, i need. Do you have maybe any suggestion, how I can use Data binding to solve my problem?
Yes, it is possible. The concept you need to learn is XAML Data Binding.
You can implement the IValueConverter for this.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters
You can check the value on binding and return the background color.
how to give different text color for each item of listview in xamarin forms

Setting today's date in XAML

I want to display today's date in XAML and then return that to my view model. So this shows it in XAML
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat=dd/MMM/yyyy}"/>
but I want to return it in ModifiedDate, so I did this
<TextBlock x:Name="ModifiedDate" Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat=dd/MMM/yyyy}"/>
This doesn't work. I don't want to use datepicker. I want to use Pure XAML and do not wish to go to code. Any ideas?
you have to include this name space in your xaml..if you are not able to show date..
xmlns:sys="clr-namespace:System;assembly=mscorlib"
and if you want something different update your question..

How to data bind based on emptiness of a collection?

I want to make a TextBlock visible only when a collection is empty. I have a ListView that is data-bound to the collection already and that's simple. I just want to display something else when the ListView is otherwise empty.
I wrote an IValueConverter that would take the collection, or count, or whatever I need, and return a Visibility appropriately. The XAML looks like this:
<TextBlock Visibility="{Binding Count, ElementName=ContactsList, Converter={StaticResource visibilityWhenEmpty}}"
Text="No contacts yet. Add one using the AppBar below." />
The trouble is that binding just the collection itself only calls my value converter once, when it's empty, and not again when the contents of the collection changes (kinda makes sense). And when, as shown above, I try binding against the collection's Count property, it doesn't call my value converter at all.
Any ideas?
I have faced the same issue. I applied a tricky solution. It might work for you as well. Taks a temporary combo box and bind the collection with that.
<ComboBox x:Name="TempComboBox"
ItemsSource="{Binding DataContext.ContactsList,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" >
<ComboBox.Template>
<ControlTemplate>
<!--Add file button..-->
<TextBlock Content="Your text..."
>
<TextBlock.Visibility>
<Binding Path="Items.Count"
RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=ComboBox}"
Converter="{StaticResource visibilityWhenEmpty}">
</Binding>
</TextBlock.Visibility>
</TextBlock>
</ControlTemplate>
</ComboBox.Template>
</ComboBox>
You could use notifyPropertyChanged for Count, or implement using BindableBase.

StringFormat in silverlight Xaml and resources

I have format strings in my resource files. I am trying to access these from the Text attribute of TextBlock using FormatString
Text="{Binding Path=Project.Name, StringFormat={Binding Path=WkStrings.DisplayProjectName, Source={StaticResource ResourceWrapper}}}"
I am getting the following error:
Provide value on 'System.Windows.Data.Binding' threw an exception
Error points to Text=.
Is it possible to access resources from a "nested binding"?
Binding.StringFormat is not a dependency property and therefore you cannot set a binding to this property. If you want to assign a value to that property, your value has to be a static resource, like this:
<TextBlock Text="{Binding ProjectName, StringFormat={StaticResource ProjectNameFormat}}"/>
You should declare your resource like this:
<UserControl.Resources>
<System:String x:Key="ProjectNameFormat">Project: {0}</System:String>
</UserControl.Resources>
The end result looks like this:
Your syntax is wrong for using StringFormat and you may want something other than StringFormat. StringFormat is used to manipulate the output of what is assigned to the Path of the Binding. In your example you're binding to the Project.Name property.
StringFormat should be used to achieve the similar effect as using String.Format in code. See this reference for formatting: http://msdn.microsoft.com/en-us/library/26etazsy(v=VS.95).aspx
Other answers around this topic:
Does Silverlight support StringFormat in binding?
http://blog.davemdavis.net/2009/12/03/silverlight-4-data-binding-string-format/
Here's some example code of using StringFormat:
<TextBlock Text="{Binding Path=Cost, StringFormat=\{0:c\}}" />

XAML support for local-name() in XPath

I'd like to bind to the element name of a node in my XmlDataProvider. I can't seem to get local-name() to work within my XPath expression. Does XAML support local-name()?
<TextBlock Text="{Binding XPath=local-name()}" />
I have been trying to do exactly the same thing and am pretty sure it is not supported in a single step.
The Binding.XPath help says
The XmlNode::SelectNodes method handles the XPath expressions from the XPath property. XPath functions are not supported.
However
You can work around it using a bit of a hack - you need a container around the element to provide a DataContext which is the result of your XPath and then you can query the LocalName property of that context object using Path, such as in my working example:
<StackPanel Grid.Row="20" Grid.Column="1"
DataContext="{Binding XPath=r:Result/r:LIC1}">
<TextBlock Text="{Binding Path=LocalName}" />
</StackPanel>
which I'd originally been trying to achieve with:
<TextBlock Grid.Row="20" Grid.Column="1"
Text="{Binding XPath=r:Result/r:LIC1/local-name\(\)}" />