How to use Converter to bind TextAlignment to string property - c#

Simply i want to make converter to bind TextAlignment option enum to string property .. i found converter is the solution after long searching but i'm still can't do specific one for my case .. any help ?
I use this code
EnumtoStringConverter.cs
class EnumtoStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var returnValue = value as string;
if (returnValue == "Start")
return "Start";
else if (returnValue == "Center")
return "Center";
else if (returnValue == "End")
return "End";
return returnValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
}
}
Xaml
<StackLayout Orientation="Horizontal" HeightRequest="40" HorizontalOptions="Fill" BackgroundColor="White">
<controls:ExtendedButton HorizontalContentAlignment="{Binding HoriRLLR}" Margin="20,0,0,0" Image="house.png" HorizontalOptions="FillAndExpand" BackgroundColor="White" Text="{translator:Translate HomeSpacing}"></controls:ExtendedButton>
</StackLayout>
HoriRLLR is the string property which i want to bind
I know i should write code in convert method but i don't know What do I miss? and if i'm in right way or not ?

You don't need to do this you can make enum property and bind direct to this property Use TextAlignment for this
your code will be like
Private Enum alignment{get;set;}
and in constructor
alignment = Textalignment.End
in xaml
horizontaloption bind in aignment property

Related

Change font color by WPF Converter

Into my xaml I have label:
<Label Content="{Binding Path=HSValidation, Converter={StaticResource HSFontConverter}}" />
Into converter I want to change label font to "RED":
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
return value; //should be value with changed color
}
return value;
}
Can I use just c# code?
OR do I need name property of my label?
Converters only convert the source value to something else, so you would only be able to convert the "Content" property to something else with your binding.
So you should have your content be bound to something as you have and then have the "Foreground" property be bound to the color.
<Label
Content="{Binding Path=HSValidation}"
Foreground="{Binding Path=HSValidation, Converter={StaticResource HSFontConverter}}" />
You should return Brushes.Red for instance in your converter code, and a Brush on all branches in the converter, not the bound value:
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
return Brushes.Red;
}
return Brushes.Black;
}

C# ConvertBack from a binding column of textbox to a JObject

GoodDay!
I'm not good with wpf and binding, i need your help. I have already bind a Json Object (JObject) to a Column of TextBox.
<TextBox Width="250" Text="{Binding Path=Property, Converter={StaticResource jPropertyConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
I can show properly the data of the Jobject when i start my wpf window, now i need to ConvertBack the data modified when i modify one of the textbox of the columns, from that TextBox to the JObject, and the related JValue.
public class JPropertyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is JProperty)
{
JToken valoreProperty = (value as JProperty).Value;
if ((valoreProperty is JValue))
return (valoreProperty as JValue).Value;
}
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
??
}
}
I can change the Value of the "leaves" of this JObject tree like:
(valoreProperty as JValue).Value = "Hello!";
How can i change the "leaves" of this JObject tree in the convert back?
Sorry for my english.
Thanks and bye
EDIT:
Thanks dbc! It works, many thanks!
Now i need to show in another column, every lenght of the value inside of the textbox, obviously if i change the value in the texbox the relative lenght value will change too.
I tried:
<DataGridTextColumn Header="Lunghezza2" IsReadOnly="True" Width="50" Binding="{Binding Path=Property.Value.Value.toString().Length, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
or in another way with a converter, passing a JProperty
public class LengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is JProperty)
{
JToken jValue = (value as JProperty).Value;
if ((jValue is JValue) && (jValue as JValue).Value != null)
return (jValue as JValue).Value.ToString().Length.ToString();
else
return "0";
}
else
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
But every try didn't works when i change the textbox value, any tips?
Many thanks again!
You don't need a converter for this purpose. Assuming your Property property returns a JProperty, you can bind directly to JProperty.Value.Value
<TextBox Name="PropertyTextBox"
Text="{Binding Path=Property.Value.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
If you want to disable the TextBox if the bound Property is not a "simple" JProperty (one with just a JValue for its value), you can do:
<TextBox Name="PropertyTextBox"
Text="{Binding Path=Property.Value.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding Path=Property, Converter={StaticResource IsSimpleJPropertyConverter}, Mode=OneWay}"
/>
Using the converter
public class IsSimpleJPropertyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is JProperty)
{
JToken jValue = (value as JProperty).Value;
if (jValue is JValue)
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

Binding user control visibility to valueconverter

I do have a ValueConverter that works fine on elements e.g. like a STACKPANEL. The Method User.OwnsRight(Int32) returns true or false.
[ValueConversion(typeof(object), typeof(System.Windows.Visibility))]
public class ConverterUserRightVisibility : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bxSuite.Base.Objects.User locUser = (bxSuite.Base.Objects.User)value;
if (parameter == null) return System.Windows.Visibility.Visible;
if (locUser == null) return System.Windows.Visibility.Visible;
if (locUser.OwnsRight(System.Convert.ToInt32(parameter)))
{
return System.Windows.Visibility.Visible;
}
else
{
return System.Windows.Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
System.Windows.Visibility locVisibility = (System.Windows.Visibility)value;
if (locVisibility == System.Windows.Visibility.Hidden || locVisibility == System.Windows.Visibility.Collapsed)
{
return false;
}
else
{
return true;
}
}
}
So this XAML works great:
<StackPanel Visibility="{Binding Path=User, Converter={StaticResource ConverterUserRightVisibility}, ConverterParameter=4}"></StackPanel>
But if i do bind the Visibility of one of my UserControls to this Converter, the Converter doesn't even get fired. The UserControl itself works fine.
<my:MenuButtonLarge Visibility="{Binding Path=User, Converter={StaticResource ConverterUserRightVisibility}, ConverterParameter=4}"/>
Any ideas why? Any help appreciated! Thanks a lot.
EDIT: i've tried another Converter simply setting text on a Label control. Works fine, but if i use it on a Textproperty (dependency property) of a usercontrol the Converter with the ConverterParameter doesn't get fired? What am I missing?

How to hide element in ListView in databinding for specific value

I have XML file and listView and I want to hide specific XAML element in this list View Item if value of XML element <Finished> equals false.
How can I do it?
You bind the XAML element's visibility to the Finished property of the list item.
You will need to bind it through a converter that takes a boolean value and returns Visibility.Visible or Visibility.Collapsed as appropriate:
<TextBlock Visibility="{Binding Finished,
Converter={StaticResource BoolToVisibilityConverter}}"/>
The converter:
public class GenderConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw NotImplementedException();
}
}
If the value isn't a boolean the converter will be slightly more complex and you may need to pass in a ConverterParameter as a control value so you can have more control over when you return Visibility.Visible

Where to set the Converter for items of a collection in XAML

I just made my first converter to convert from int to string. I have a combobox fill with integers(years) but if the value is 0 I want the combobox to show 'All'.
This is my converter:
public class IntToString : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
int intY = (int)value;
if (intY == 0)
{
String strY = "All";
return strY;
}
else
{
return intY.ToString();
}
}
return String.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
}
}
In XAML where should I set the converter ? I tried in the ItemsSource of the combobox:
ItemsSource="{Binding YearsCollection, Converter={StaticResource intToStringYearConverter}}"
But I always get InvalidcastException on this line:
int intY = (int)value;
The problem is that you are trying to convert the entire collection rather than just one item from the collection.
You would want to do something like this:
<ListBox ItemsSource="{Binding YearsCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border DataContext="{Binding Converter={StaticResource intToStringYearConverter}">
...
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You can't use the converter like this, converter in ItemsSource is supposed to convert whole collection, not individual items. The collection object can't be cast to integer, so you get the exception.
You have to use DataTemplate and apply the converter on individual items.
Or - if all you need is cast to int - you could use ItemStringFormat.
Also, for setting the default message when the source is null, you can use TargetNullValue property of a Binding.

Categories

Resources