Creating a bindable Point in C# WPF

后端 未结 4 2167
执念已碎
执念已碎 2021-02-15 23:51

I know multiple inheritence is out, but is there a way to create a wrapper for System.Windows.Point that can inherit from it but still implement bindable dependency properties?<

4条回答
  •  天命终不由人
    2021-02-15 23:55

    Unfortunately, Point is a struct, and structs don't support inheritance. From MSDN

    Note Structs do not support inheritance, but they can implement interfaces. For more information, see Interfaces (C# Programming Guide).

    Maybe this doesn't answer your question directly but you can easily bind a Point with the help of a Converter. In your case it would be like

    
        
            
            
                                                
    
    

    PointConverter

    public class PointConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double xValue = (double)values[0];
            double yValue = (double)values[1];
            return new Point(xValue, yValue);
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    If you just want to bind the X value and have a static Y value you could do this like

    
    

    PointXConverter

    public class PointXConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double progressBarValue = (double)value;
            double yValue = System.Convert.ToDouble(parameter);
            return new Point(progressBarValue, yValue);
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    } 
    

提交回复
热议问题