Boolean to Visibility Converter - inverted? - c#

how do i do something like this
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
<WrapPanel>
<TextBlock Text="{Binding ElementName=ConnectionInformation_ServerName,Path=Text}"/>
<Image Source="Images/Icons/Select.ico" Margin="2" Height="15" Visibility="{Binding SQLConnected,Converter={StaticResource BoolToVis},ConverterParameter=true}"/>
<Image Source="Images/Icons/alarm private.ico" Margin="2" Height="15" Visibility="{Binding SQLConnected,Converter={StaticResource BoolToVis},ConverterParameter=false}"/>
</WrapPanel>
is there a way to use the Boolean to vis converter but inverted without writing a whole method in C to do it?
or should i just have these images overlap and hide one when i need to ?

As far as I know, you have to write your own implementation for this. Here's what I use:
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool boolValue = (bool)value;
boolValue = (parameter != null) ? !boolValue : boolValue;
return boolValue ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
And I generally set ConverterParameter='negate' so it's clear in code what the parameter is doing. Not specifying a ConverterParameter makes the converter behave like the built-in BooleanToVisibilityConverter. If you want your usage to work, you can, of course, parse the ConverterParameter using bool.TryParse() and react to it.

From #K Mehta (https://stackoverflow.com/a/21951103/1963978), with slight updates for the method signature for Windows 10 Universal applications (Changing from "CultureInfo culture" to "string language", per https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh701934.aspx) :
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, string language)
{
bool boolValue = (bool)value;
boolValue = (parameter != null) ? !boolValue : boolValue;
return boolValue ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
throw new NotImplementedException();
}
}

Related

NullToVisibilityConverter make visible if not null

Want to hide and show property grid for SelectedItem in listview
<UserControl xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
<ListView>
<!--here is list view-->
</ListView>
<xctk:PropertyGrid SelectedObject="{Binding Active}" Visibility="{Binding Active, Converter=NullToVisibilityConverter}" >
</xctk:PropertyGrid>
</UserControl>
So I need converter and use it in visibility property converter. Any help?
public class NullVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? Visibility.Hidden : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Then reference the NullVisibilityConverter in your XAML Resources.
<StackPanel.Resources>
<simpleXamlContent:NullVisibilityConverter x:Key="NullToVisibilityConverter"/>
</StackPanel.Resources>
To use the converter we can create one in the resources, and refer to it as a static resource in the binding statement.
<UserControl xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit">
<UserControl.Resources>
<yournamespace:NullVisibilityConverter x:Key="NullToVisibilityConverter"/>
</UserControl.Resources>
<ListView>
<!--here is list view-->
</ListView>
<xctk:PropertyGrid SelectedObject="{Binding Active}" Visibility="{Binding Active, Converter={StaticResource NullToVisibilityConverter}}" >
</xctk:PropertyGrid>
</UserControl>
and Converter class itself
public class NullVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? Visibility.Hidden : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
There is little more useful version allows to set default invisibility value:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string defaultInvisibility = parameter as string;
Visibility invisibility = (defaultInvisibility != null) ?
(Visibility)Enum.Parse(typeof(Visibility), defaultInvisibility)
: Visibility.Collapsed;
return value == null ? invisibility : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
Where ever in resources add:
<converters:NullReferenceToVisibilityConverter x:Key="NullToVis" />
And use it like there:
<StackPanel Visibility="{Binding MyObject, Converter={StaticResource NullToVis}}">
<StackPanel Visibility="{Binding MyObject, Converter={StaticResource NullToVis}, ConverterParameter=Hidden}">

How can we handle Null Values while Binding data to Text block in Windows phone

Hear i Have a confusion while Binding the data to Text Block in Windows phone
I have Text Block
<TextBlock Name="strytxt"
Text="{Binding STORY}"
Height="auto"
Width="Auto"
TextWrapping="Wrap"/>
in STORY Object some time I have Empty/Null Values
At that Time im Getting Some space in my UI
Now i Want to Make Visibility of the Textbox in to Collapsed if i get Null in that row
How can i do this
To change the Visibility of the TextBlock when the Binding value is null you need to use a Converter that converts from null/not null to Visible/Collapsed.
HereĀ“s a converter that converts the values. The converter handles an empty string as null, so that it return Collapsed for string.empty.:
public class NullToVisibilityConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
bool isVisible = value == null ? false : true;
if (isVisible) {
string stringValue = value as string;
if (stringValue != null) {
isVisible = string.IsNullOrEmpty(stringValue) ? false : true;
}
}
if (System.ComponentModel.DesignerProperties.IsInDesignTool) {
return Visibility.Visible;
}
return isVisible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
}
You can apply the converter to the TextBlock as follows:
<UserControl.Resources>
<local:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
</userControl.Resources>
<TextBlock Name="strytxt"
Visibility="{Binding STORY, Converter={StaticResource nullToVisibilityConverter}}"/>
Another alternativ is to display a Text when the value is null, you can specify that in the binding
<TextBlock Name="strytxt"
Text="{Binding STORY, TargetNullValue='is Null'}"/>
You can use a value converter to convert the value to a visibility:
public class NullToVisibiltyConverter : IValueConverter {
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture) {
return value == null ? Visibility.Collapsed : Visibility.Visible;
}
public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}
You then bind the Visibility of the TextBlock using the converter:
<TextBlock Name="strytxt"
Text="{Binding STORY}"
Height="auto"
Width="Auto"
TextWrapping="Wrap"
Visibility="{Binding STORY, Converter={StaticResource NullToVisibilityConverter}}"/>
You need to add an instance of the converter to a resource dictionary to be able to reference it in the binding:
<UserControl.Resources>
<local:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
</userControl.Resources>

Set converter on TextBox

I need to calculate/change form of input between textbox to its bindable source. The way i trying to achive this, is with help of converters.
Converter:
public class ParameterConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return string.Empty;
string originalParameValue = value.ToString();
string fixedParameterValue = string.Format("#_{0}", originalParameValue);
return fixedParameterValue;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
XAML:
<Window.Resources>
<converters:ParameterConverter x:Key="parameterConverter" />
</Window.Resources>
<Grid>
<TextBox Text="{Binding ParameterA, Converter={StaticResource parameterConverter}}"/>
</Grid>
The problem is, that converter is functioning only once. Is it approach correct (i mean with converter) or there are another approaches ?
Perhaps the binding Mode is not two way, and did your property fire property changed.
Does your data context implement INotifyPropertyChanged and is PropertyChanged being called whenever ParameterA is changed? It seems as if nobody is notifying the textbox that it needs to update its contents.

Hide empty textBlock in windows phone 7?

How to make a textBlock auto hide if empty in windows phone 7 app (C#, silverlight, xaml)?
I know there's a similar question for WPF but it seems not applicable in silverlight.
You can use a converter:
<TextBlock Visibility="{Binding YourString, Converter={StaticResource LengthConverter}" />
<UserControl.Resources>
<converter:LengthConverter x:Key="LengthToVisibilityConverter" />
</UserControl.Resources>
Then the converter is:
public class LengthToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string text = (string)value;
return text.Length > 0 ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
You could make this slightly cleaner by binding to the text length directly:
<TextBlock Visibility="{Binding YourString.Length, Converter={StaticResource LengthConverter}" />
In which case the converter becomes:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int length = (int)value;
return length > 0 ? Visibility.Visible : Visibilty.Collapsed;
}
Find out more about Converters here: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter(v=vs.110).aspx

WPF RadioButton InverseBooleanConverter not working

I have two RadioButtons which I am binding to a boolean property in the ViewModel. Unfortunately I am getting an error in the converter because the 'targetType' parameter is null.
Now I wasn't expecting the targetType parameter coming through to be null (I was expecting True or False). However I noticed that the IsChecked property of the RadioButton is a nullable bool so this kind of explains it.
Can I correct something in the XAML or should I be changing the solution's existing converter?
Here's my XAML:
<RadioButton Name="UseTemplateRadioButton" Content="Use Template"
GroupName="Template"
IsChecked="{Binding UseTemplate, Mode=TwoWay}" />
<RadioButton Name="CreatNewRadioButton" Content="Create New"
GroupName="Template"
IsChecked="{Binding Path=UseTemplate, Mode=TwoWay, Converter={StaticResource InverseBooleanConverter}}"/>
This is the existing converter I am using solution wide InverseBooleanConverter:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((targetType != typeof(bool)) && (targetType != typeof(object)))
{
throw new InvalidOperationException("The target must be a boolean");
}
return !(((value != null) && ((IConvertible)value).ToBoolean(provider)));
}
You need to change the converter, or what's probably even better, use a new converter.
[ValueConversion(typeof(bool?), typeof(bool))]
public class Converter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType != typeof(bool?))
{
throw new InvalidOperationException("The target must be a nullable boolean");
}
bool? b = (bool?)value;
return b.HasValue && b.Value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
#endregion
}

Categories