IValueConverter and visibility

I have an application with 3 messages: Connected, Disconnected and Pending. Communication status is monitored by several other parameters. I want to display the corresponding image on a screen controlled by IValueConverter. But I can’t make it work.

Heres is my Xaml code for including three images:

<Image x:Name="connectedImage"  
    Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateToVisibilityConverter}, ConverterParameter=ConverterParameterConnected}"
    Source="Assets/connected.png"
    Stretch="None"
    HorizontalAlignment="Center" />



<Image x:Name="disconnectedImage"
    Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateToVisibilityConverter}, ConverterParameter=ConverterParameterDisconnected}"
    Source="Assets/disconnect.png"
    Stretch="None"
    HorizontalAlignment="Center" />


<Image x:Name="pendingImage"
    Visibility="{Binding ConnectionWithServerEstablished, Converter={StaticResource communitationStateToVisibilityConverter}, ConverterParameter=ConverterParameterPending}"
    Source="Assets/pending.png"
    Stretch="None"
    HorizontalAlignment="Center" />

CommunosState Management Method Used Here

public enum CommunitationState { Connected, Disconnected, Pending }

public CommunitationState ConnectionWithServerEstablished
{
    get
    {
        if (IRCommandSent)
            return CommunitationState.Disconnected;

        if (wifiConnected && !fcConnected)
            return CommunitationState.Pending;

        return wifiConnected ? CommunitationState.Connected : CommunitationState.Disconnected;
    }
}

And last but not least, the converter:

  public class CommunitationStateToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var result = Visibility.Collapsed;

            if ((string)parameter == "ConverterParameterConnected")
                result = (CommunitationState)value == CommunitationState.Connected ? Visibility.Visible : Visibility.Collapsed;

            if ((string)parameter == "ConverterParameterDisconnected")
                result = (CommunitationState)value == CommunitationState.Disconnected ? Visibility.Visible : Visibility.Collapsed;

            if ((string)parameter == "ConverterParameterPending")
                result = (CommunitationState)value == CommunitationState.Pending ? Visibility.Visible : Visibility.Collapsed;
            Debug.WriteLine("value={0}, parameter={1}, result={2}", value,   (string)parameter, result);
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

Working with Databinding works as intended. I know this for sure because I have a text box associated with another method that displays the state as text. My converter is being called, I know for sure, because I can place a breakpoint on it.

So, something is wrong with my converter, because I always end up with a compensated image.

**** ****

debug.Writeline

:

value=Connected, parameter=ConverterParameterConnected, result=Visible
value=Connected, parameter=ConverterParameterDisconnected, result=Collapsed
value=Connected, parameter=ConverterParameterPending, result=Collapsed

:

value=Pending, parameter=ConverterParameterConnected, result=Collapsed
value=Pending, parameter=ConverterParameterDisconnected, result=Collapsed
value=Pending, parameter=ConverterParameterPending, result=Visible

:

value=Connected, parameter=ConverterParameterConnected, result=Visible
value=Connected, parameter=ConverterParameterDisconnected, result=Collapsed
value=Connected, parameter=ConverterParameterPending, result=Collapsed
value=Pending, parameter=ConverterParameterConnected, result=Collapsed
value=Pending, parameter=ConverterParameterDisconnected, result=Collapsed
value=Pending, parameter=ConverterParameterPending, result=Visible

, , , TCP-, Wi-Fi, .

+2
1

, , ConnectionWithServerEstablished , / PropertyChanged, .

, , :

public bool IRCommandSent
{
    set
    {
        // set the value
        // ...

        // notify event listeneers that the ConnectionWithServerEstablished may have changed
        if (PropertyChanged != null)
        {
             PropertyChanged(this, new PropertyChangedEventArgs("ConnectionWithServerEstablished"));
        }
    }
}

, DataContext ( ViewModel), , , INotifyPropertyChanged .

+1
source

Source: https://habr.com/ru/post/1688964/


All Articles