V
V
veydlin2019-05-07 09:36:26
WPF
veydlin, 2019-05-07 09:36:26

Why doesn't Converter work?

Gives an error message

Failed to resolve BoolToVisibilityConverter resource

The funny thing is that from the project where I copied everything is fine, and before that I used this code everywhere and it worked
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>

Moreover, VS says that the local space is not required and suggests deleting
xmlns:local="clr-namespace:WellnessDevice.DataBinding"

In which direction to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2019-05-07
@veydlin

<Window.Resources>
      <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</Window.Resources>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question