StringFormat does not work in Silverlight Control - silverlight-4.0

I have a piece of code for a Text Box in a XAML File. The text box takes as input Numeric Value.Below is the piece of code:-
<TextBox Text="{Binding Path=Revenue, StringFormat=c0, Mode=TwoWay, Converter={StaticResource NullableConverter}, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Grid.Column="3" Grid.Row="2"/>
However if I input 5 and tab out the input does not become 5 $. However, on saving the data in the page and loading the page the formatting is intact. Any thoughts on this ??

please remove the converter and try it, you should get it. should be something wrong with converter.
this is what I tried for Nullable Converter it worked.
public class NullableConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return 0;
else
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}

If you are using SL5 I think this is a bug, the StringFormat is not triggered when the binding is updated. For more details here is the MS connect issue. You can check the workaround it might help you solving the problem.

Related

Xamarin Style staticresource binding

would it be possible to databind a static resource in Xamarin Forms?
Something like
Style="{StaticResource {Binding foo, StringFormat='SomeStyle{0}'}}"
Thanks
What you probably want is a Value Converter that handles locating the StaticResource for you. Full Microsoft Documentation here.
On your XAML element, you'd do something like this:
<Entry Style="{Binding foo, Converter={StaticResource FooToStyleConverter}}"/>
Your converter would work something like this:
public class FooToStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var someValue = (string)value; // Convert 'object' to whatever type you are expecting
// evaluate the converted value
if (someValue != null && someValue == "bar")
return (Style)App.Current.Resources["StyleOne"]; // return the desired style
return (Style)App.Current.Resources["StyleTwo"];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// Usually unused, but inverse the above logic if needed
throw new NotImplementedException();
}
}
Lastly, set up your converter as a Static Resource in App.xaml (or as a local resource on the page) so your page can properly reference it
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingDemos">
<ContentPage.Resources>
<ResourceDictionary>
<local:FooToStyleConverter x:Key="FooToStyleConverter" />
....

CheckBox binding to element issue in WP8.1

I have a simple XAML:
<CheckBox x:Name="chkShowGrid" IsThreeState="False" IsChecked="False">Show content</CheckBox>
<Grid Visibility="{Binding IsChecked, ElementName=chkShowGrid}">
<TextBlock>Some content goes here</TextBlock>
</Grid>
This XAML works well in WinRT Windows 8.1 application. When I trying it in WP8.1 application it works in designer (shows or hides grid depend of checkbox value), but not on the phone. Why?
UPDATE:
I have universal 8.1 app and if use converter then shows me exception:
error CS0012: The type 'Type' is defined in an assembly that is not
referenced. You must add a reference to assembly 'System.Runtime,
Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Converter code:
class BoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null) return false;
return (bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
In XAML
<Page.Resources>
<local:BoolConverter x:Name="ThatsMyConverter"/>
</Page.Resources>
........
<Grid Visibility="{Binding IsChecked, ElementName=chkShowSettings,Converter={StaticResource ThatsMyConverter}}">
Your converter returns a boolean value and you are trying to assign a bool to Visibility which can only take Visible or Collapsed as possible values. Try returning Visibility from your converter instead of bool. That should work. For example in your converter:
if (value == null) return Visibility.Collapses;
var val = (value as bool);
if (val)
return Visibility.Visible;
else
return Visibility.Collapsed;

Binding to Element height Plus a value

I wanna bind my element height to another element height plus 20 pixel...
Is it possible to do such a thing?
<ScrollViewer Height="{Binding Height, ElementName=AnotherElement}">
Yes, by using a value converter
public class IncreaseByValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var passedInValue = System.Convert.ToDouble(value);
var increaseByValue = System.Convert.ToDouble(parameter);
return passedInValue + increaseByValue;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
defined as a static resource somewhere in your resources:
<local:IncreaseByValueConverter x:Key="IncreaseByValueConverter" />
used like this:
<ScrollViewer Height="{Binding Height, ElementName=AnotherElement, Converter={StaticResource IncreaseByValueConverter}", ConverterParameter="20">
You'll probably want to handle the converter parameter better by using some try/catch code in the IncreaseByValueConverter.Convert method. For example, make the parameter optional, so if it's not passed in, use a default value, or something like that...

StringFormat in XAML

I am trying to format my string to have commas every 3 places, and a decimal if it is not a whole number. I have checked roughly 20 examples, and this is the closest I have come:
<TextBlock x:Name="countTextBlock" Text="{Binding Count, StringFormat={0:n}}" />
But I get a The property 'StringFormat' was not found in type 'Binding'. error.
Any ideas what is wrong here? Windows Phone 8.1 appears to differ from WPF, because all of the WPF resources say that this is how it is done.
(The string is updated constantly, so I need the code to be in the XAML. I also need it to remain binded. Unless of course I cannot have my cake and eat it too.)
It seems that, similar to Binding in WinRT, Binding in Windows Phone Universal Apps doesn't have StringFormat property. One possible way to work around this limitation is using Converter as explained in this blog post,
To summarize the post, you can create an IValueConverter implmentation that accept string format as parameter :
public sealed class StringFormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null)
return null;
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();
}
}
Create a resource of above converter in your XAML, then you can use it like this for example :
<TextBlock x:Name="countTextBlock"
Text="{Binding Count,
Converter={StaticResource StringFormatConverter},
ConverterParameter='{}{0:n}'}" />

Reference to current class xaml Windows Phone

I got the following code:
namespace SomeApp
{
public partial class MyClass : PhoneApplicationPage, IValueConverter
{
SOME METHODS...
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return true;
}
}
}
I would like to bind this class to a ValueConverter of a RadioButton. Is there any way to reference to the current class I'm working with? For example:
<phone:PhoneApplicationPage
x:Class="SomeApp.MyClass"
xmlns:local="clr-namespace:SomeApp">
<phone:PhoneApplicationPage.Resources>
<local:MyClass x:Key="myClass"/>
</phone:PhoneApplicationPage.Resources>
<RadioButton IsChecked="{Binding Converter={StaticResource myClass}}"/>
Thanks in advance =)
First using your page as a converter don't seem like a good idea, it's better practice to separate the converter fonctionality in a separate class. Particularly it would be a very bad idea to create a StaticResources of a converter created that way as it will use a lot of unessessary memory to create the whole page.
The only thing a converter can be binded in xaml is a StaticResource so you will not be able to do it in xaml but if you really want to do it you could do it by creating the binding from code behind (for example in the constructor of the page):
Binding binding=new Binding();
binding.Converter = this;
myRadioButton.SetBinding(CheckBox.IsCheckedProperty, binding);