IValueConverter和可见性 [英] IValueConverter and Visibility

查看:100
本文介绍了IValueConverter和可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有3个通信状态的应用程序:已连接,已断开连接和待处理.通信状态由其他一些参数控制.我想在由IValueConverter控制的屏幕上显示相应的图像.但是我无法使其正常工作.

I have an application with 3 comunicationstates: Connected, Disconnected and Pending. The communication state are controled by some other paramers. I want to display the corresponding image on the screen controlled by an IValueConverter. But I cant get it to work.

这是我的Xaml代码,其中包含3张图片:

Heres is my Xaml code for includeing the 3 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" />

这是控制CommunitationState的方法

Here is the methos controlling CommunitationState

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

数据绑定的工作与其预期的一样.我知道,肯定是因为我有一个文本框绑定了另一种将状态显示为文本的方法.我的转换器被调用了,我肯定知道,因为我可以在其中放置一个断点.

The Databinding works like its supposed to do. I know that for sure bacause I have a textbox bound an other method displaying the state as text. My converter gets called, I know that for sure because I can place a break point in it.

所以我的转换器出了点问题,因为我总是以崩溃的图像结尾.

So there is something wrong with my converter, because I allways ends up with a collapsed image.

****编辑****

**** EDIT ****

这是我的debug.Writeline的一些输出

Here are some output from my 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服务器,但仍然可以访问Wifi,因此我将状态更改为待定".

This is correct because my program defaults to connected, and after a second it realizes that it can not see the TCP server but still have acces to Wifi so i changes state to pending.

推荐答案

根据您的评论,很可能您的ConnectionWithServerEstablished属性没有更改以使图像可见和/或您未触发PropertyChanged属性值更改时发生的事件.

From your comments, it is most likely that your ConnectionWithServerEstablished property does not change to make the images visible and/or you do not fire a PropertyChanged event when the property value changed.

例如,可以通过在依赖属性的设置器中触发事件来做到这一点:

You can do this for example by firing the event in the setter of your dependent properties:

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.

The class you use as DataContext (your ViewModel) must of course implement INotifyPropertyChanged for that.

这篇关于IValueConverter和可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆