I want to control the visibility of an image (inside a dynamically bind longlistselector control), depending on a binding value (say if somevalue>0 then make that image visible otherwise invisible).But there is no such event like itemdatabound in the longlistselector to accomplish this task, I am new to windows phone development, and really don't have an idea how to do this.Please help me guys.
Thanks,
Use a ValueConverter
public class BoolToVisiblityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value
? Visibility.Collapsed
: Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
A common way to achieve this would be to bind the visibility property of the Image to a property on your bound data object. Often, the property on the data would be a boolean and a converter would be used to convert the boolean to a Visibility enum value. E.g.,
<Image Visibility = {Binding IsVisible, Converter={StaticResource myBoolToVisibilityConverter} />
See binding with converters example
Related
I have a class of IValueConverter with a property named myValue which I want to divide the ivalueconverter by its property myValue! But, I want to know if it is possible to pass the myValue from xamarin page to ivalueconverter? If yes, how?
IvalueConverter
class salesUIbtnWidth : IValueConverter
{
public double myValue { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double)value / myValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Try Implementing the class
<Button Text="Click me" HorizontalOptions="Center" VerticalOptions="Center"
CornerRadius="15" WidthRequest="{Binding Source={x:Reference
frame}, Path=Width,Converter={StaticResource salesUIbtnWidth}}">
</Button>//how to bind also the MyValue
All I want is to know how to set myValue in page and pass it to (IvalueConverter) from xamarin during runtime!!
You can pass the value by ConverterParameter.
You can refer to the sample code:
<Label Text="{Binding Red,
Converter={StaticResource doubleToInt},
ConverterParameter=255,
StringFormat='Red = {0:X2}'}" />
<local:DoubleToIntConverter x:Key="doubleToInt" />
DoubleToIntConverter.cs
public class DoubleToIntConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)Math.Round((double)value * GetParameter(parameter));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value / GetParameter(parameter);
}
double GetParameter(object parameter)
{
if (parameter is double)
return (double)parameter;
else if (parameter is int)
return (int)parameter;
else if (parameter is string)
return double.Parse((string)parameter);
return 1;
}
}
For more, you can check: Binding Converter Parameters
According to your question this is what you need to do!
Following #Jessie Zhang example
<Button HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"
HeightRequest="{Binding Source={x:Reference frame}, Path=Height,
Converter={StaticResource doubleToInt}, ConverterParameter=enterYOURvaluHERE}"></Button>
If you don't want to use parameter, you can define MyValue when you create the converter.
<ContentPage.Resources>
<app1:MyConverter x:Key="myConverter" MyValue="255"/>
</ContentPage.Resources>
Please question might be funny, not be funny at all or confusing! But the simple goal I wanted is to changing the value of binding context in xamarin.forms on runtime!!
IvalueConverter
class LoginFrameHeight : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (double)value / 1.9;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Resource Dictionary
<ResourceDictionary>
<local:LoginFrameHeight x:Key="frameHeight"/>
</ResourceDictionary>
Setting Frame Height
<Frame CornerRadius="15"
HeightRequest="{Binding Source={x:Reference frame},
Path=Height,Converter={StaticResource frameHeight}}" Padding="0"></Frame>
Code works fine! My problem is that I have about three (3) frames to apply different HeightRequested using same process! is it possible to change the (1.9) in IvalueConverter during
runtime, so that I can use the same class LoginFrameHeight instead of creating different classes for the frames?
The answer is "YES" you can use ConverterParameter here is quick ref: This official documentation will show more
Example:<Label Text="{Binding Red,Converter={StaticResource doubleToInt}, ConverterParameter=255, StringFormat='Red = {0:X2}'}" />
I have implemented a Xamarin Forms SearchHandler in my Shell project. I want to be able to "hide" it (and unhide it). There is no IsVisible property on it - any ideas how to hide the SearchHandler control?
I figured it out - the property is actually SearchBarVisibility.
In my case, I created a value converter to and bound SearchBarVisibility to a property in my ViewModel.
My ValueConverter looks like this:
public class SearchVisibleConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SearchBoxVisibility searchBoxVisibility = SearchBoxVisibility.Expanded;
if (value != null)
{
switch (value)
{
case "Hidden":
searchBoxVisibility = SearchBoxVisibility.Hidden;
break;
case "Collapsible":
searchBoxVisibility = SearchBoxVisibility.Collapsible;
break;
default:
searchBoxVisibility = SearchBoxVisibility.Expanded;
break;
}
}
return searchBoxVisibility;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
My xaml looks like this:
<Shell.SearchHandler>
<controls:RouteSearchHandler
x:Name="RouteSearch"
BackgroundColor="White"
DisplayMemberName="Street1"
SearchBoxVisibility="{Binding TopSearchVisibility, Converter={StaticResource visibleConvert}}"
ShowsResults="True" />
</Shell.SearchHandler>
It happens that I define a ResourceDictionary for the app colors to use it in XAML files, and have a static class for these colors to use in the cs code:
for example:
<ResourceDictionary
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Themes.AppTheme">
<Color x:Key="BrandColor">#ffd92e</Color>
<Color x:Key="BgColor">#343433</Color>
</ResourceDictionary>
And the opposite class:
public static class AppColors
{
public static readonly Color BrandColor = (Color)Application.Current.Resources["BrandColor"];
public static readonly Color BGColor = (Color)Application.Current.Resources["BgColor"];
}
Another scenario,I use icons font in both xaml and cs,
in XAML it looks like , and in cs it's: \ue8d5 . I want to save them in a file where I can reference them by a meaningful names in XAML and cs like:
IconBell = \ue8d5
Is it possible to define resources like those in one place and use them in both XAML and code?
About the colors issue, it's very easy to solve it. You basically have to create a class containing your colors, and reference it in your XAML, with the x:Static extension.
You can do the same for solving your icons issue, but you'll probably need to create a converter for using it in XAML. For example, the important part of your icon value is the "e8d5" part, but in C# you use the "\u" and in the XAML you use the "&#x". You'll have to create a class with the icons, reference it in the XAML using the x:Static extension and use a converter to convert from C# to XAML, for example:
public class IconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
string icon = System.Convert.ToString(value);
return icon.Replace("\u", "&#x");
}
catch (Exception)
{
throw new InvalidCastException("The value is not a valid string.");
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
With this, you can unify your styles in a unique C# class, and just reference it in your XAML.
I'm trying to bind a TextBlock to a TimeSpan, but I need to format is so that if TotalMinutes is less then 60 it should show "X min" else it should show "X h".
Its it possible? That might require tom logic test in the xaml?
You should use custom IValueConverter implementation. There are several tutorials about that, e.g. Data Binding using IValueConverter in Silverlight.
Your IValueConverter implementation should look like that:
public class TimeSpanToTextConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is TimeSpan))
throw new ArgumentException("value has to be TimeSpan", "value");
var timespan = (TimeSpan) value;
if (timespan.TotalMinutes > 60)
return string.Format("{0} h", timespan.Hours.ToString());
return string.Format("{0} m", timespan.Minutes.ToString());
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}