How to print a textBlock using conditionals in XAML? - c#

I need to print in a textBlock repeat if a number is less than 80 and paint color red, and more or equal than 80 print successful with green color.
How can I do that in XAML?

Converters.
Sadly there are no inequality-triggers or the like, so using a converter should do.
<TextBlock>
<TextBlock.Foreground>
<Binding Path="TestDouble">
<Binding.Converter>
<vc:ThresholdConverter BelowValue="{x:Static Brushes.Red}"
AboveValue="{x:Static Brushes.Green}"
Threshold="80" />
</Binding.Converter>
</Binding>
</TextBlock.Foreground>
<TextBlock.Text>
<Binding Path="TestDouble">
<Binding.Converter>
<vc:ThresholdConverter BelowValue="Repeat"
AboveValue="Successful"
Threshold="80" />
</Binding.Converter>
</Binding>
</TextBlock.Text>
</TextBlock>
public class ThresholdConverter : IValueConverter
{
public double Threshold { get; set; }
public object AboveValue { get; set; }
public object BelowValue { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double input;
if (value is double)
{
input = (double)value;
}
else
{
var converter = new DoubleConverter();
input = (double)converter.ConvertFrom(value);
}
return input < Threshold ? BelowValue : AboveValue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}

<local:NumberToBrushConverter x:Key="numberToBrushConverter" />
<local:NumberToTextConverter x:Key="numberToTextConverter" />
<TextBlock Background="{Binding Number, Converter={StaticResource numberToBrushConverter}}"
Text="{Binding Number, Converter={StaticResource numberToTextConverter}"/>
class NumberToBrushConverter: IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int number = (int)value;
return number < 80 ? Brushes.Red : Brushes.Green;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
#endregion
}
The other converter would look similar to the brush converter, but return "Successful" or "Repeat".

Related

How to bind 2 textboxes to one property?

I have a propery PhoneNumber and in the UI, I have 2 textboxes, one is the prefix, and the other one is the postfix, how can I bind it to the property? (The property inside the DataContext).
<TextBox Grid.Column="0" MaxLength="3" /> //Prefix
<TextBlock Grid.Column="1" Text="-" />
<TextBox Grid.Column="2" /> //Postfix
The only way I see it work is with code behind using textbox1.Text + textbox2.Text... Is there a better way?
Thanks in advance :)
Just use two more properties in the data context
code is not complied or tested
public string PhoneNumber { get; set; }
public string Prefix
{
get
{
return PhoneNumber.Substring(0, 3);
}
set
{
// replace the first three chars of PhoneNumber
PhoneNumber = value + PhoneNumber.Substring(3);
}
}
public string Postfix
{
get
{
return PhoneNumber.Substring(3);
}
set
{
// replace the chars of starting from index 3 of PhoneNumber
PhoneNumber = PhoneNumber.Substring(0, 3) + value;
}
}
I think uou can use Converter for this purpose, the example going one way can look like this:
In this my Number is a string 000-000000, but you can surely change it.
In XAML:
<Window.Resources>
<conv:PostixConverter x:Key="PostfixConv" xmlns:conv="clr-namespace:Example.Converters"/>
<conv:PrefixConverter x:Key="PrefixConv" xmlns:conv="clr-namespace:Example.Converters"/>
</Window.Resources>
<StackPanel>
<TextBox MaxLength="3" Text="{Binding Number, Converter={StaticResource PrefixConv}}"/>
<TextBlock Text="-" />
<TextBox Text="{Binding Number, Converter={StaticResource PostfixConv}}"/>
</StackPanel>
And in code behind:
namespace Example.Converters
{
public class PrefixConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return null;
else return ((string)value).Substring(0, 3);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class PostixConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return null;
else return ((string)value).Substring(4);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

IValueConverter in ResourceDictionary

I'm trying to use a Converter inside a ResourceDictionary. That's the code I have:
<Window x:Class="Metro.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cnv="clr-namespace:Metro.converters">
<Window.Resources>
<cnv:DarkenColorConverter x:Key="Darken" />
<Color x:Key="Red">#FF0000</Color>
<SolidColorBrush Color="{StaticResource Red}"
x:Key="Accent" />
<SolidColorBrush Color="{Binding Source={StaticResource Red}, Converter={StaticResource ResourceKey=Darken}}"
x:Key="DarkAccent" />
</Window.Resources>
<StackPanel>
<Grid Background="{StaticResource Accent}">
<TextBlock>grid 1</TextBlock>
</Grid>
<Grid Background="{StaticResource DarkAccent}">
<TextBlock>grid 2</TextBlock>
</Grid>
</StackPanel>
</Window>
Here's the converter:
public class DarkenColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Brushes.Blue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Brushes.Gray;
}
}
But somehow it's not working. As soon as I use the converter inside the Grid directly, everything works fine:
<Grid Background="{Binding Source={StaticResource Red}, Converter={StaticResource ResourceKey=Darken}}">
<TextBlock>grid 2</TextBlock>
</Grid>
What`s wrong with the first xaml sample?
In the first conversion you are converting a Color, the one in the Grid is converting a SolidColorBrush.
You will have to modify your converter to accept Color also.
public class DarkenColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double percentage = 0.8;
if (parameter != null)
{
double.TryParse(parameter.ToString(), out percentage);
}
if (value is SolidColorBrush)
{
Color color = (value as SolidColorBrush).Color;
return new SolidColorBrush(Color.FromRgb((byte)(color.R * percentage), (byte)(color.G * percentage), (byte)(color.B * percentage)));
}
else if (value is Color)
{
Color color = (Color)value;
return Color.FromRgb((byte)(color.R * percentage), (byte)(color.G * percentage), (byte)(color.B * percentage));
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
The problem was the wrong converter return type.
Working converter:
public class DarkenColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Colors.Blue;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Colors.Gray;
}
}

IValueConverter with Bound Dependency Properties

I need to determine the StringFormat of some bound TextBlocks at runtime based on the unit system identified in the object to be bound.
I Have a converter with a Dependency Property that I would like to Bind to. The Bound value is used in determining the conversion process.
public class UnitConverter : DependencyObject, IValueConverter
{
public static readonly DependencyProperty IsMetricProperty =
DependencyProperty.Register("IsMetric", typeof(bool), typeof(UnitConverter), new PropertyMetadata(true, ValueChanged));
private static void ValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
((UnitConverter)source).IsMetric = (bool)e.NewValue;
}
public bool IsMetric
{
get { return (bool)this.GetValue(IsMetricProperty); }
set { this.SetValue(IsMetricProperty, value); }
}
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (IsMetric)
return string.Format("{0:0.0}", value);
else
return string.Format("{0:0.000}", value);
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
I declare the converter
<my:UnitConverter x:Key="Units" IsMetric="{Binding Path=IsMetric}"/>
and bind the TextBlock
<TextBlock Text="{Binding Path=Breadth, Converter={StaticResource Units}}" Style="{StaticResource Values}"/>
Never the less, I get the following error:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=IsMetric; DataItem=null; target element is 'UnitConverter' (HashCode=62641008); target property is 'IsMetric' (type 'Boolean')
I guess this is initialising before I set the datacontext and therefore there is nothing to bind the IsMetric property to. How can I achieve the desired result?
Provided that Breadthand IsMetric are properties of the same data object, you might use a MultiBinding in conjunction with a multi value converter:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource UnitMultiValueConverter}">
<Binding Path="Breadth" />
<Binding Path="IsMetric" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
with a converter like this:
public class UnitMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
double value = (double)values[0];
bool isMetric = (bool)values[1];
string format = isMetric ? "{0:0.0}" : "{0:0.000}";
return string.Format(format, value);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
The problem with your approach is that when the UnitConverter is declared as resource it does not have a DataContext, and it will never get one later on.
And one more important thing: the ValueChanged callback for UnitConverter.IsMetric is nonsense. It sets the same property again which was just changed.

How to calculate a value in WPF Binding

I have an app which uses two sliders to generate a product used elsewhere in the code. What I would like is to have the product value bound to a textblock or tooltip, for example, to look something like "10 x 15 = 150".
The first part is easy, and looks like this:
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} x {1}">
<Binding ElementName="amount_slider" Path="Value" />
<Binding ElementName="frequency_slider" Path="Value"/>
</MultiBinding>
</TextBlock.Text>
But what's a nice easy way to get the product in there as well?
Using Pavlo Glazkov's solution, I modified it to look like this:
public class MultiplyFormulaStringConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var doubleValues = values.Cast<double>().ToArray();
double x = doubleValues[0];
double y = doubleValues[1];
var leftPart = x.ToString() + " x " + y.ToString();
var rightPart = (x * y).ToString();
var result = string.Format("{0} = {1}", leftPart, rightPart);
return result;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And the all-important
<Window.Resources>
<local:MultiplyFormulaStringConverter x:Key="MultiplyFormulaStringConverter"/>
</Window.Resources>
Thanks!
Instead of using StringFormat create a converter. Something like this:
public class MultiplyFormulaStringConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var doubleValues = values.Cast<double>().ToArray();
var leftPart = string.Join(" x ", doubleValues);
var rightPart = doubleValues.Sum().ToString();
var result = string.Format("{0} = {1}", leftPart, rightPart);
return result;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<TextBlock.Text>
<MultiBinding Converter="{StaticResource MultiplyFormulaStringConverter}">
<Binding ElementName="amount_slider" Path="Value" />
<Binding ElementName="frequency_slider" Path="Value"/>
</MultiBinding>
</TextBlock.Text>
You could use a converter and pass as a parameter the two values that you would like to calculate. The converter would do the calculation and then return the string result.
(Converter example here)

C# WPF IsEnabled using multiple bindings?

I have a WPF xaml file describing a section of a GUI and I'd like the enabling/disabling of a particular control to be dependent on two others. The code looks something like this at the moment:
<ComboBox Name="MyComboBox"
IsEnabled="{Binding ElementName=SomeCheckBox, Path=IsChecked}"/>
But I'd like it to be dependant on another checkbox as well so something like:
<ComboBox Name="MyComboBox"
IsEnabled="{Binding ElementName=SomeCheckBox&AnotherCheckbox, Path=IsChecked}"/>
What's the best way to go about that? I can't help feeling I'm missing something obvious or going about this the wrong way?
You can use a MultiBinding with a converter which implements IMultiValueConverter.
Just to give an answer you can (almost) copy&paste:
Static resource needed:
<converterNamespace:BooleanAndConverter x:Key="booleanAndConverter" />
The ComboBox:
<ComboBox Name="MyComboBox">
<ComboBox.IsEnabled>
<MultiBinding Converter="{StaticResource booleanAndConverter}">
<Binding ElementName="SomeCheckBox" Path="IsChecked" />
<Binding ElementName="AnotherCheckbox" Path="IsChecked" />
</MultiBinding>
</ComboBox.IsEnabled>
</ComboBox>
The code for the converter:
namespace ConverterNamespace
{
public class BooleanAndConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
foreach (object value in values)
{
if ((value is bool) && (bool)value == false)
{
return false;
}
}
return true;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException("BooleanAndConverter is a OneWay converter.");
}
}
}
You can also try shorter version of the same:
public class BooleanAndConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return values.OfType<IConvertible>().All(System.Convert.ToBoolean);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
public class BooleanOrConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return values.OfType<IConvertible>().Any(System.Convert.ToBoolean);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
and, of course, you may need the converters for visibility, too:
public class BooleanOrToVisibilityConverter : IMultiValueConverter
{
public Visibility HiddenVisibility { get; set; }
public bool IsInverted { get; set; }
public BooleanOrToVisibilityConverter()
{
HiddenVisibility = Visibility.Collapsed;
IsInverted = false;
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
bool flag = values.OfType<IConvertible>().Any(System.Convert.ToBoolean);
if (IsInverted) flag = !flag;
return flag ? Visibility.Visible : HiddenVisibility;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class BooleanAndToVisibilityConverter : IMultiValueConverter
{
public Visibility HiddenVisibility { get; set; }
public bool IsInverted { get; set; }
public BooleanAndToVisibilityConverter()
{
HiddenVisibility = Visibility.Collapsed;
IsInverted = false;
}
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
bool flag = values.OfType<IConvertible>().All(System.Convert.ToBoolean);
if (IsInverted) flag = !flag;
return flag ? Visibility.Visible : HiddenVisibility;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
I believe you may have to use a MultiBinding with a MultiValueConverter. See here: http://www.developingfor.net/wpf/multibinding-in-wpf.html
Here is a directly related example: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5b9cd042-cacb-4aaa-9e17-2d615c44ee22
As extension to qqbenq's answer:
Added the function to handle the Count of a Collection for example if you want to check if some item of a ListView is selected.
Converter:
public class IsEnabledConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
foreach (var value in values)
{
switch (value)
{
case bool b when !b:
case int i when i == 0:
return false;
}
}
return true;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return null;
}
}
Namespace <theNamespace:IsEnabledConverter x:Key="IsEnabledConverter"/>
Button
<Button x:Name="MyButton">
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource IsEnabledConverter}">
<Binding ElementName="MyListView" Path="SelectedItems.Count"/>
<Binding ElementName="MyCheckBox" Path="IsChecked"/>
</MultiBinding>
</Button.IsEnabled>
</Button>
When you don't want to use MultiBinding
public class AndEnabledTextBox : TextBox
{
public static readonly DependencyProperty AndEnabled1SubProperty =
DependencyProperty.Register(nameof(AndEnabled1), typeof(bool), typeof(AndEnabledTextBox), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnAndEnabledPropertyChanged)));
public static readonly DependencyProperty AndEnabled2SubProperty =
DependencyProperty.Register(nameof(AndEnabled2), typeof(bool), typeof(AndEnabledTextBox), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnAndEnabledPropertyChanged)));
public bool AndEnabled1 { get { return (bool)GetValue(AndEnabled1SubProperty); } set { SetValue(AndEnabled1SubProperty, value); } }
public bool AndEnabled2 { get { return (bool)GetValue(AndEnabled2SubProperty); } set { SetValue(AndEnabled2SubProperty, value); } }
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
IsEnabled = AndEnabled1 && AndEnabled2;
}
protected static void OnAndEnabledPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
AndEnabledTextBox textBox = (AndEnabledTextBox)obj;
textBox.IsEnabled = textBox.AndEnabled1 && textBox.AndEnabled2;
}
}
XMAL is more simplified when you use inherited controls.
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1">
<StackPanel>
<ToggleButton x:Name="switch1" Content="{Binding ElementName=switch1, Path=IsChecked}"/>
<ToggleButton x:Name="switch2" Content="{Binding ElementName=switch2, Path=IsChecked}"/>
<local:AndEnabledTextBox Text="TEXT"
AndEnabled1="{Binding ElementName=switch1, Path=IsChecked, Mode=OneWay}"
AndEnabled2="{Binding ElementName=switch2, Path=IsChecked, Mode=OneWay}"/>
</StackPanel>
</Window>

Categories

Resources