Change font color by WPF Converter - c#

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;
}

Related

How to use Converter to bind TextAlignment to string property

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

Allow NULL in a WPF Extended Toolkit DecimalUpDown control

I have a property on my view model of type decimal?. NULL should be a valid value for this property, but when I erase the text from the DecimalUpDown control, a validation error occurs and the property is not given the value NULL (whatever it was previously remains).
The control is declared in xaml like:
<xctk:DecimalUpDown ValueChanged="UpDownBase_OnValueChanged" Text="{Binding ServiceSize}" Minimum="0" Grid.Column="4" Grid.Row="2" Margin="5" IsEnabled="{Binding IsEditable}"/>
It will bind correctly if I enter a number
But as soon as the number is erased a validation error occurs, and the value can't be set back to NULL (in this case the model still has "5" as the value for "ServiceSize").
Validation.GetHasError() returns true for this control. Can I remove the Validation Rules entirely somehow?
You can implement an IValueConverter to handle empty input.
public class DecimalUpDownValueConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
// handle input on a case-to-case basis
if(value == null)
{
// Do something
return 0;
}
else
{
return value;
}
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
// Do the conversion from model property to DecimalUpDownValue
return value;
}
}
On your view: (Assuming you added the DecimalUpDownValueConverter as a static resource)
<xctk:DecimalUpDown ValueChanged="UpDownBase_OnValueChanged" Text="{Binding ServiceSize, Converter = { StaticResource DecimalUpDownValueConverter }}" Minimum="0" Grid.Column="4" Grid.Row="2" Margin="5" IsEnabled="{Binding IsEditable}"/>

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

Categories

Resources