Binding user control visibility to valueconverter - c#

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?

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

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

Binding ToggleButton IsChecked property to another ToggleButton not working

I have two ToggleButtons.
I need to bind one IsChecked property to the other ToggleButton.
I use a custom Converter to inverse the value.
However, it's not working? Here's my code:
XAML:
<ToggleButton
IsChecked="{Binding Path=ToggleButton.IsChecked, ElementName=menuCatUit,
Converter={StaticResource InvertBool}}"/>
<ToggleButton x:Name="menuCatUit" IsChecked="True" />
Code:
[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
return !(bool)value;
}
}
Remove the "ToggleButton" from the path property.
You only need the property name.
<ToggleButton
IsChecked="{Binding Path=IsChecked, ElementName=menuCatUit,
Converter={StaticResource InvertBool}}"/>
Here is what i Do, I send them all to the same Checked and UnChecked event:
privateToggleButton Toggle;
public void ToggleButtonChecked(object sender, RoutedEventArgs e)
{
if (Toggle != null && Toggle != sender as ToggleButton)
{
Toggle.IsChecked = false;
}
Toggle = sender as ToggleButton;
}
public void ToggleButtonUnChecked(object sender, RoutedEventArgs e)
{
if (Toggle != null)
{
Toggle = null;
}
}
And then to check which ToggleButton is checked do something like this
switch (Toggle.Name)
{
case ("Button1"): /*MyCode*/ break;
};
Found the solution:
I needed to edit my converter because ToggleButton uses a nullable bool.
[ValueConversion(typeof(bool?), typeof(bool))]
public class InverseNullableBool : IValueConverter
{
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);
}
}

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

WPF Binding does not update Button-Content

May someone tell me whats wrong with this sourcecode?
when i click the button its not updating ist value?
At first binding the converter makes his job.
the sourcecode is pretty big so i will post only some snippets.
XAML:
Instances is type of ObservableCollection
<ListBox Name="Instances">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Tag="{Binding Path=Instance.Name}" Content="{Binding Path=Instance.Active, Converter={StaticResource BTSC}}" Click="ChangeAccess"/>
<TextBlock Text="{Binding Path=Instance.Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Converter:
public class BoolToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (((Boolean)value) == true)
return "No";
else
return "Yes";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Event:
private void ChangeAccess(object sender, RoutedEventArgs e)
{
for...
if ((sender as Button).Tag.ToString() == (DP.Instances[i].Instance as CInstance).Name)
{
SkipIfAndElse...
DP.Instances[i].Instance.Active = true;
}
}
CInstance:
class CInstance : INotifyPropertyChanged
{
private Boolean active;
public Boolean Active
{
get { return active; }
set
{
active = value;
NotifyPropertyChanged("Access");
}
}
}
All other values of the CInstance class are updating as expected.
In your CInstance class
NotifyPropertyChanged("Access");
should be
NotifyPropertyChanged("Active");
I would suggest you start using some kind of INPC framework. I personally like Simon Cropp's Fody.
Fody adds the appropriate OnNotifyPropertyChanged as a post compilation step, which means you don't get the Runtime hit that you get with Expression based solutions.
At the end of the day, string based OnNotifyPropertyChanged are all pretty fragile.

Categories

Resources