locked
How do you set the stroke thickness as a percentage of the size of the shape? RRS feed

  • Question

  • I need to create a user control that will be resized in places that reference it.  The stroke thinkness should be proportionate to the size of the control.  Can't seem to find a way to do that.

    Friday, January 8, 2010 1:32 AM

Answers

  • Hi YiLeiW,

    Your existing code was run at the control initialization time, which the width was 0.

    The implementation should be using databinding and valueConvertor

    In the XAML file set the binding:

    StrokeThickness="{Binding ActualWidth, Converter={StaticResource DoubleConverter}, ElementName=pathName,}"
    In the code behind write a valueConvertor:

    using System.Windows.Data;
    
     [ValueConversion(typeof(object),typeof(double))]
        public class DoubleConverter: IValueConverter
        {
            public object Convert(object value, Type targetType, 
                object parameter, System.Globalization.CultureInfo culture)
            {
                Nullable<double> v = value as Nullable<double>;
                if (v != null)
                {
                    return v * 0.1;
                }
                else
                {
                    return v;
                }
            } 
    
            public object ConvertBack(object value, Type targetType, 
                object parameter, System.Globalization.CultureInfo culture)
            {
                // we don't intend this to ever be called
                return null; 
            }
        }
    
    

    Thanks, Harry Huang [MSFT]
    • Proposed as answer by Harry.MSFT Saturday, January 23, 2010 8:39 AM
    • Marked as answer by YileiW Tuesday, January 26, 2010 8:02 PM
    Saturday, January 23, 2010 8:39 AM

All replies

  • Can you use code-behind?
    Friday, January 8, 2010 7:58 AM
  • I tried to put the code in the user control's constructor:

    public

     

     

    btn_Remove(){

     

     

    this.InitializeComponent();

    Path.StrokeThickness = Path.Width *

     

    0.1;

    }


    And drag this user control in the WPF app.  The stroke doesn't show at all.  What did I do wrong?

    Friday, January 8, 2010 7:24 PM
  • Hi YiLeiW,

    Your existing code was run at the control initialization time, which the width was 0.

    The implementation should be using databinding and valueConvertor

    In the XAML file set the binding:

    StrokeThickness="{Binding ActualWidth, Converter={StaticResource DoubleConverter}, ElementName=pathName,}"
    In the code behind write a valueConvertor:

    using System.Windows.Data;
    
     [ValueConversion(typeof(object),typeof(double))]
        public class DoubleConverter: IValueConverter
        {
            public object Convert(object value, Type targetType, 
                object parameter, System.Globalization.CultureInfo culture)
            {
                Nullable<double> v = value as Nullable<double>;
                if (v != null)
                {
                    return v * 0.1;
                }
                else
                {
                    return v;
                }
            } 
    
            public object ConvertBack(object value, Type targetType, 
                object parameter, System.Globalization.CultureInfo culture)
            {
                // we don't intend this to ever be called
                return null; 
            }
        }
    
    

    Thanks, Harry Huang [MSFT]
    • Proposed as answer by Harry.MSFT Saturday, January 23, 2010 8:39 AM
    • Marked as answer by YileiW Tuesday, January 26, 2010 8:02 PM
    Saturday, January 23, 2010 8:39 AM