开发者

What's wrong with my binding?

I'm working on an application using a TCP protocol. I want to show the different statuses of the communication in different colors (connect = green, disconnect = red)

I defined an enum:

public enum ComunicationStateTypeEnum
    {
        COMUNICATION_CONNECTED,

        COMUNICATION_DISCONNECTED

    };

I defined a conversion class:

namespace Conversion
{
[ValueConversion(typeof(ComunicationStateTypeEnum), typeof(Brushes))]
public class ComStatusToColor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        ComunicationStateTypeEnum state = (ComunicationStateTypeEnum)value;
        if (state == ComunicationStateTypeEnum.COMUNICATION_CONNECTED)
            return Brushes.Green;
        return Brushes.Red;            
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }

}

}

In Xaml I defined an ellipse:

 <Ellipse Name="ComEllipse" Height="25" Width="30" Fill ="{Binding Path=eCommStatus, Converter={StaticResource ComStatusToColor}}" Stroke="Black" DockPanel.Dock="Left"/>

also in Xaml I defined:

xmlns:ConversionNamespace="clr-namespace:Conversion"    
<Window.Resources>        
    <ConversionNamespace:ComStatusToColor x:Key="ComStatusToColor"/>
</Window.Resources>

I want to bind to an existing object, 开发者_如何转开发therefore I initiated:

ComEllipse.DataContext = SystemLogic.GetInstance();

(SystemLogic is a singleton)

and in SystemLogic I defined:

public class SystemLogic
{
public ComunicationStateTypeEnum eCommStatus { get; set; }
...
}

eCommStatus is initiated to COMUNICATION_DISCONNECTED in the constructor and the ellipse turns red, and still when eCommStatus member changes to COMUNICATION_CONNECTED , the ellipse doesn't change its color

What's wrong?

Gil


You need to implement INotifyPropertyChanged interface in your SystemLogic class to let UI know that a property's value has changed. This way UI can update itself.

Example

public class SystemLogic : INotifyPropertyChanged
{
    private ComunicationStateTypeEnum _eCommStatus;
    public ComunicationStateTypeEnum eCommStatus
    {
        get
        {
            return _eCommStatus;
        }
        set
        {
            _eCommStatus = value;
            RaisePropertyChanged("eCommStatus");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler temp = PropertyChanged;
        if (temp != null)
        {
            temp(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}


Nothing wrong with your binding. But SystemLogic must implement INotifyPropertyChanged to report back when it is changed.

public class SystemLogic
{
    private ComunicationStateTypeEnum _eCommStatus;
    public ComunicationStateTypeEnum eCommStatus 
    { 
        get{return _eCommStatus;} 
        set
        {
            _eCommStatus = value;
            OnPropertyChanged("ComunicationStateTypeEnum");
        }
    }
...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜