Answer the question
In order to leave comments, you need to log in
Why doesn't Converter work?
Gives an error message
Failed to resolve BoolToVisibilityConverter resource
using System;
using System.Windows;
using System.Windows.Data;
// Visibility="{Binding isSet, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=true}"
namespace WellnessDevice.DataBinding {
public class BoolToVisibilityConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
var flag = false;
if (value is bool) {
flag = (bool)value;
} else if (value is bool?) {
var nullable = (bool?)value;
flag = nullable.GetValueOrDefault();
}
if (parameter != null) {
if (bool.Parse((string)parameter)) {
flag = !flag;
}
}
if (flag) {
return Visibility.Visible;
} else {
return Visibility.Collapsed;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
var back = ((value is Visibility) && (((Visibility)value) == Visibility.Visible));
if (parameter != null) {
if ((bool)parameter) {
back = !back;
}
}
return back;
}
}
}
<Window x:Class="WellnessDevice.View.MainView"
xmlns:local="clr-namespace:WellnessDevice.DataBinding"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="WellnessDevice" Height="290" Width="400">
<Grid>
<Label Content="Девайс 1" Visibility="{Binding device1Status.isDeviceConnect, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=true}"/>
</Grid>
</Window>
xmlns:local="clr-namespace:WellnessDevice.DataBinding"
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question