Wpf中的动态转换器

时间:2010-07-07 10:20:42

标签: c# wpf mvvm

我有tabcontrol。

在第一个tabitem我有十进制类型的坐标。

在第二个tabitem我有DMS类型的坐标。

当用户更改其中一个内容时,我希望此更改会影响第二种类型

现在它只适用于我运行应用程序控件时填充笛卡尔坐标。但是当我在以十进制坐标运行后更改某些内容时,笛卡尔坐标的控件没有任何变化

class DecimalCoordinatesToCartesianConverter : IValueConverter
    {
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }
            else
            {

                DecimalCoordinates decimalCoorinates = (DecimalCoordinates)value;

                CartesianCoorinates cartesianCoorinates = new CartesianCoorinates();

                // Set flag if number is negative
                cartesianCoorinates.LatitudeDirection = (decimalCoorinates.Latitude < (decimal)0d) ? GeoDirectionLatitude.S : GeoDirectionLatitude.N;

                // Work with a positive number
                decimalCoorinates.Latitude = Math.Abs(decimalCoorinates.Latitude);

                // Get d/m/s components
                decimal tempLatitudeDegress = Math.Floor(rdecimalCoorinates.Latitude);
               decimalCoorinates.Latitude -= tempLatitudeDegress;
               decimalCoorinates.Latitude *= 60;
                decimal tempLatitudeMinutes = Math.Floor(decimalCoorinates.Latitude);
                decimalCoorinates.Latitude -= tempLatitudeMinutes;
                decimalCoorinates.Latitude *= 60;
                decimal tempLatitudeSeconds = Math.Round(decimalCoorinates.Latitude);

                // Create padding character
                char padLatitude;
                char.TryParse("0", out padLatitude);


                cartesianCoorinates.LatitudeHours = (int)tempLatitudeDegress;
                cartesianCoorinates.LatitudeMinutes = System.Convert.ToInt32(tempLatitudeMinutes.ToString().PadLeft(2, padLatitude));
                cartesianCoorinates.LatitudeSeconds = System.Convert.ToInt32(tempLatitudeSeconds.ToString().PadLeft(2, padLatitude));


                cartesianCoorinates.LongitudeDirection = (decimalCoorinates.Longitude < (decimal) 0d)
                                                                     ? GeoDirectionLongtitude.W
                                                                     : GeoDirectionLongtitude.E;

                // Work with a positive number
                decimalCoorinates.Longitude = Math.Abs(decimalCoorinates.Longitude);

                // Get d/m/s compon## Heading ##ents
                decimal tempLongitudeDegress = Math.Floor(decimalCoorinates.Longitude);
                decimalCoorinates.Longitude -= tempLongitudeDegress;
                decimalCoorinates.Longitude *= 60;
                decimal tempLongitudeMinutes = Math.Floor(decimalCoorinates.Longitude);
                decimalCoorinates.Longitude -= tempLongitudeMinutes;
                decimalCoorinates.Longitude *= 60;
                decimal tempLongitudeSeconds = Math.Round(decimalCoorinates.Longitude);

                // Create padding character
                char padLongitude;
                char.TryParse("0", out padLongitude);


                cartesianCoorinates.LongitudeHours = (int)tempLongitudeDegress;
               cartesianCoorinates.LongitudeMinutes = System.Convert.ToInt32(tempLongitudeMinutes.ToString().PadLeft(2, padLongitude));
                cartesianCoorinates.LongitudeSeconds = System.Convert.ToInt32(tempLongitudeSeconds.ToString().PadLeft(2, padLongitude));



                return cartesianCoorinates;



            }
        }


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


public class HotelDescriptionModelView
    {
        HotelDescription hotelDescription { get; set; }

        public HotelDescriptionModelView()
        {
            hotelDescription = new HotelDescription();

            hotelDescription.DecimalCoordinates = new DecimalCoordinates() { Latitude = (decimal)-89.67, Longitude = (decimal)-89.67 };
        }
        public DecimalCoordinates DecimalCoordinates
        {
            get
            {
                return this.hotelDescription.DecimalCoordinates;
            }
            set
            {
                this.hotelDescription.DecimalCoordinates = value;
            }
        }

        public CartesianCoordinates CartesianCoordinates
        {
            get
            {
                return this._hotelDescription.CartesianCoordinates;
            }
            set
            {
                this._hotelDescription.CartesianCoordinates = value;
            }
        }

    }


  public class CartesianCoordinates
    {
        public int LatitudeHours { get; set;}
        public int LatitudeMinutes { get; set; }
        public int LatitudeSeconds { get; set; }
        public GeoDirectionLongtitude LongitudeDirection { get; set; }

        public int LongitudeHours { get; set; }
        public int LongitudeMinutes { get; set; }
        public int LongitudeSeconds { get; set; }
        public GeoDirectionLatitude LatitudeDirection { get; set; }
    }

public class DecimalCoordinates
    {
        public decimal Latitude { get; set; }
        public decimal Longitude { get; set; }
    }

public enum GeoDirectionLongtitude
    {
        W=0,
        E=1
    }

public enum GeoDirectionLatitude
    {
        N=0,
        S=1
    }

1 个答案:

答案 0 :(得分:1)

您需要在类中实现INotifyPropertyChanged,并在属性更改时触发事件。否则WPF不知道值是不同的。