Set IsVisible property by a string value of model - xaml

I am using XAML to define a ListView, with multiple buttons for each cell.
I want to trigger visibility depending on whether a string value is empty or not.
My button inside the ListView is:
<Button Text="{Binding Phone}"
Clicked="OnPhoneClicked"
CommandParameter="{Binding Telefono}"
x:Name="btnPhone" />
Binding Phone is read from my model. It is correctly shown.
How can set a IsVisible property button if Phone's value is an empty string?

Try this code
<Button Text="{Binding Phone}"
Clicked="OnPhoneClicked"
CommandParameter="{Binding Telefono}"
x:Name="btnPhone"
IsVisible="{Binding Phone,Converter={StaticResource StringNullOrEmptyBoolConverter"} />
StringNullOrEmptyBoolConverter.cs file
public class StringNullOrEmptyBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var s = value as string;
return !string.IsNullOrWhiteSpace(s);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Finally add this in App.xaml file
<Application.Resources>
<ResourceDictionary>
<Converter:StringNullOrEmptyBoolConverter x:Key="StringNullOrEmptyBoolConverter" />
</ResourceDictionary>
</Application.Resources>

Related

Converter is called twice, second time with null value

I am writing a MAUI app. I am trying to use a converter. But for some reason, the converter is called twice, and that makes the app crash. Here is the code:
xaml:
<Picker Title="Choose Tile Thickness..."
ItemsSource="{Binding TileThicknessesMetric}"
ItemDisplayBinding="{Binding Value}"
SelectedItem="{Binding SelectedTileThickness}"
IsVisible="{Binding SelectedUnit, Converter={converters:SelectedUnitToBoolShowMetricConverter}, Mode=TwoWay}"
Style="{StaticResource PickerStyle}"
Grid.Row="2" Grid.Column="1" />
Converter:
class SelectedUnitToBoolShowMetricConverter : IValueConverter, IMarkupExtension
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((Unit)value).Code == UnitCode.Metric;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
public object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
As I mentioned, Convert() is called twice, and the second time value is null, which causes a null reference exception. I cannot figure out why it is called the second time.
The same code works as expected in a Xamarin.Forms app.

Xamarin forms URL binding on Image

I have a Image in my xaml in which the source is a URL that Iam binding.The URL from json will be like this : "/images/Uploads/e0111.png". I have the URL in my Common values stored class as CommonValues.URL. How can I add this "CommonValues.URL" before the json at the time of binding? So that the source for Image will be http://example.com//images/Uploads/e0111.png.?
If you need Uri
var myUrl= new Uri(CommonValues.URL + "images/Uploads/e0111.png");
If string than
var myUrl=CommonValues.URL + "images/Uploads/e0111.png";
Or you can do it like this in your ViewModel or Page
public string Url => string.Format("{0}{1}", CommonValues.URL,"/images/Uploads/e0111.png");
Then in XAML:
<Button Text="{Binding Url}"/>
You can use a converter, which will allow you to reuse in all your views/application
public class UrlConverter : IValueConverter
{
#region IValueConverter implementation
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var test = value as string;
if (!string.IsNullOrEmpty(test))
{
return CommonValues.URL + test;
}
return false;
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
#endregion
}
Then, in your page:
<ContentPage.Resources>
<ResourceDictionary>
<converter:UrlConverter x:Key="UrlConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<Image Source="{Binding YourProperty, Converter={StaticResource UrlConverter}}"/>

XAML TextBox - IValueConverter ConvertBack is not called

I have a TextBox bound to a Converter. Convert ist working fine, ConvertBack is never called.
Shouldn't ConvertBack be called after the TextBox.TextChanged Event ?
Following Code is a simplified example of my problem.
<Window.Resources>
<converter:Converter x:Key="MyConverter"/>
</Window.Resources>
<StackPanel>
<TextBox Name="TestTextBox" VerticalAlignment="Top" Margin="10"
Text="{Binding Path=.,
Converter={StaticResource MyConverter},
ConverterParameter=Name,
UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}" />
</StackPanel>
Simple ValueConverter
public class Converter : IValueConverter
{
public object Convert(object value, Type targetType, object
parameter, CultureInfo culture)
{
return "Yeahhhh";
}
public object ConvertBack(object value, Type targetType, object
parameter, CultureInfo culture)
{
return "F#*ยง";
}
}

Binding property to control

How do I bind SourceObject and TargetObject to the TextBox-Element?
This works, but I want more than one textbox and that does not seem to be possible when they are named the same.
My goal is to have the TextBox change its background color when focused.
<TextBox xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:Avalonia.Xaml.Interactivity;assembly=Avalonia.Xaml.Interactivity"
xmlns:ia="clr-namespace:Avalonia.Xaml.Interactions.Core;assembly=Avalonia.Xaml.Interactions"
x:Class="Test.View.CustomTextBox"
Name="textBox">
<i:Interaction.Behaviors>
<ia:EventTriggerBehavior EventName="GotFocus" SourceObject="{Binding #textBox}">
<ia:ChangePropertyAction TargetObject="{Binding #textBox}" PropertyName="Background" Value="{StaticResource FocusedBackgroundColor}"/>
</ia:EventTriggerBehavior>
</i:Interaction.Behaviors>
</TextBox>
Thanks a lot!
You can use RelativeSource and converter, something like that:
public class BoolColorBrushConverter : IValueConverter
{
public Brush TrueBrush {get;set;}
public Brush FalseBrush {get;set;}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is bool b && b)
return TrueBrush;
else
return FalseBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotSupportedException();
}
xaml:
<MyControl>
<MyControl.Resources>
<BoolBrushConverter TrueColor="Red" FalseColor="Blue" x:Key="TextBoxFocusedBackgroundConverter"/>
</MyControl.Resources>
<TextBox Background="{Binding IsFocused, RelativeSource={RelativeSource Self}, Converter={StaticResource TextBoxFocusedBackgroundConverter}}}"/>;
</MyControl>

Xamarin Forms show/hide option for entry

Currently I am working on Xamarin.Forms and wondering about any possibility to add show/hide option to an entry field?
I have solved a similar issue by using an expand/collapse icon above a number of entry fields.
The show/hide element in XAML
Add a clickable image with fixed size(20x20) referring to embedded resources in the PCL:
<Image Source="{Binding ShowHideIcon, Converter={StaticResource StringToResImageSourceConverter}}" WidthRequest="20" HeightRequest="20"">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ShowHideCommand}" />
</Image.GestureRecognizers>
</Image>
The ViewModel processes the command:
Switch the boolean every time the image is touched.
public bool EntryVisible { get; set; }
public Command ShowHideCommand{
get {
return new Command((object o) => {
EntryVisible = !EntryVisible;
if (EntryVisible) {
ShowHideIcon = "ic_collapse";
} else {
ShowHideIcon = "ic_expand";
}
}
}
}
The label and Entry in XAML
Bind the IsVisible attribute of the Label and Entry to the boolean in the ViewModel.
<Label Text="Quantity" IsVisible="{Binding EntryVisible}" />
<Entry Text="{Binding Quantity}" IsVisible="{Binding EntryVisible}" />
For completeness sake, I have used https://developer.xamarin.com/guides/xamarin-forms/working-with/images/#Embedded_Images to store images ic_expand.png and ic_collapse.png in the PCL Resources folder.
A Converter is required to turn a string e.g. "ic_expand" into an image reference that XAML can use.
public class StringToResImageSourceConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var resString = (string)value;
if (!string.IsNullOrEmpty(resString)) {
return ImageSource.FromResource("ProjectName.Resources." + resString + ".png");
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
Entry entry = new Entry();
// Hide it
entry.IsVisible = false;