Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Scaling woes

Scaling woes

Scheduled Pinned Locked Moved WPF
wpfcsharpcsswcfhelp
16 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Ray Hayes

    Tom, thanks for your reply. Your suggestions make a lot of sense and have, I feel, pointed me in the right direction. Although I think I'm messing up on step 6! This is what I've done so far:

    TJoe wrote:

    1. Create a separate class (e.g. ScaleFactor) with a single property (e.g. Value) 2. Implement INotifyPropertyChanged in the ScaleFactor class and fire it when the Value changes.

    public class VisualScale : INotifyPropertyChanged
    {
        private double scale = 1.0;
        public double ScalingFactor
        {
            get { return scale; }
            set { scale = value; NotifyPropertyChanged("Scale changed"); }
        }

    #region INotifyPropertyChanged Members
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }

    TJoe wrote:

    3. Create an IMultiValueConverter that takes two values: 1st will be the "value" from your underlying data (you don't have to use a TypeConverter, but you probably can), 2nd will be the scale factor. The IMultiValueConverter will then multiply the two values and return that.

    public class WidthScaler : IMultiValueConverter
    {
        #region IMultiValueConverter Members
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ( targetType != typeof(double) )
            {
                throw new NotSupportedException("Target type of WidthScaler must be double");
            }

    if ( values.Length != 2 ||
       &nb

    R Offline
    R Offline
    Ray Hayes
    wrote on last edited by
    #4

    I'm going to reply to myself as looking at the code I wrote I'd made an obvious mistake. Note, I still get the same exception but the following code is now used:

    <Slider x:Name="scaleSlider" Width="100" Minimum="0.5" Maximum="10.0"
            Value="{Binding ElementName=scale, Path=ScalingFactor}"/>

    and

    <Border.Width>
        <MultiBinding Converter="{StaticResource widthScaler}"
                     ConverterParameter="">
            <Binding Path="{Binding Duration}" />
            <Binding ElementName="scale" Path="ScalingFactor"/>
        </MultiBinding>
    </Border.Width>

    Hopefully this is now a little closer to the correct solution! Is it? :-\

    Regards, Ray

    T 1 Reply Last reply
    0
    • R Ray Hayes

      I'm going to reply to myself as looking at the code I wrote I'd made an obvious mistake. Note, I still get the same exception but the following code is now used:

      <Slider x:Name="scaleSlider" Width="100" Minimum="0.5" Maximum="10.0"
              Value="{Binding ElementName=scale, Path=ScalingFactor}"/>

      and

      <Border.Width>
          <MultiBinding Converter="{StaticResource widthScaler}"
                       ConverterParameter="">
              <Binding Path="{Binding Duration}" />
              <Binding ElementName="scale" Path="ScalingFactor"/>
          </MultiBinding>
      </Border.Width>

      Hopefully this is now a little closer to the correct solution! Is it? :-\

      Regards, Ray

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #5

      Hi Ray, Following the exception, this line is bad:

      <Binding Path="{Binding Duration}" />

      So here you are binding the Path property of a Binding object to the Value stored in the Duration property. You are basically double defining your binding statement. This needs to be:

      <Binding Path="Duration" />

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      R 1 Reply Last reply
      0
      • T TJoe

        Hi Ray, Following the exception, this line is bad:

        <Binding Path="{Binding Duration}" />

        So here you are binding the Path property of a Binding object to the Value stored in the Duration property. You are basically double defining your binding statement. This needs to be:

        <Binding Path="Duration" />

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        R Offline
        R Offline
        Ray Hayes
        wrote on last edited by
        #6

        Ok, that makes sense too. When I run (or indeed the designer does the same). I've now got an exception being fired by the WidthScaler

        if ( values.Length != 2 ||
            values[0].GetType() != typeof(double) ||
            values[1].GetType() != typeof(double) )
        {
            throw new NotSupportedException("Source values should be a pair of doubles");
        }

        The debugger shows that the first value, Duration, is correct. But the second, is DependancyProperty.UnsetValue. If I remove the throwing of an exception and return null, the WidthScaler (the IMultiValueConverter) doesn't seem to be called again!

        Regards, Ray

        T 1 Reply Last reply
        0
        • R Ray Hayes

          Ok, that makes sense too. When I run (or indeed the designer does the same). I've now got an exception being fired by the WidthScaler

          if ( values.Length != 2 ||
              values[0].GetType() != typeof(double) ||
              values[1].GetType() != typeof(double) )
          {
              throw new NotSupportedException("Source values should be a pair of doubles");
          }

          The debugger shows that the first value, Duration, is correct. But the second, is DependancyProperty.UnsetValue. If I remove the throwing of an exception and return null, the WidthScaler (the IMultiValueConverter) doesn't seem to be called again!

          Regards, Ray

          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #7

          Sorry, your second binding is incorrect also. You have:

          <Binding ElementName="scale" Path="ScalingFactor"/>

          But "scale" is not a named element, it is a resource. To access it, you need to set the Binding.Source property like so:

          <Binding Source="{StaticResource scale}" Path="ScalingFactor"/>

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          R 1 Reply Last reply
          0
          • T TJoe

            Sorry, your second binding is incorrect also. You have:

            <Binding ElementName="scale" Path="ScalingFactor"/>

            But "scale" is not a named element, it is a resource. To access it, you need to set the Binding.Source property like so:

            <Binding Source="{StaticResource scale}" Path="ScalingFactor"/>

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            R Offline
            R Offline
            Ray Hayes
            wrote on last edited by
            #8

            Great! The application now runs, but moving the slider doesn't seem to result in the MultiBinding being called again (break-pointing doesn't stop the code).

            Regards, Ray

            T 1 Reply Last reply
            0
            • R Ray Hayes

              Great! The application now runs, but moving the slider doesn't seem to result in the MultiBinding being called again (break-pointing doesn't stop the code).

              Regards, Ray

              T Offline
              T Offline
              TJoe
              wrote on last edited by
              #9

              Did you update the binding there as well? It has the same problem as before. Make sure to look at the Output window of Visual Studio, that will have any binding errors listed. Also, make sure the Binding for the slider has Mode=TwoWay.

              Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

              R 1 Reply Last reply
              0
              • T TJoe

                Did you update the binding there as well? It has the same problem as before. Make sure to look at the Output window of Visual Studio, that will have any binding errors listed. Also, make sure the Binding for the slider has Mode=TwoWay.

                Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                R Offline
                R Offline
                Ray Hayes
                wrote on last edited by
                #10

                I've now got this, but it still doesn't look like it works: Xaml is now:

                <Slider x:Name="scaleSlider" Width="100" Minimum="0.5" Maximum="10.0" Value="{Binding Source=scale, Path=ScalingFactor, Mode=TwoWay}"/>

                I'd changed the binding in the MultiBinding to:

                <Binding Source="{StaticResource scale}" Path="ScalingFactor"/>

                Error in Output window is: System.Windows.Data Error: 35 : BindingExpression path error: 'ScalingFactor' property not found on 'object' ''String' (HashCode=-528916476)'. BindingExpression:Path=ScalingFactor; DataItem='String' (HashCode=-528916476); target element is 'Slider' (Name='scaleSlider'); target property is 'Value' (type 'Double') I'm sure the error will help, once I understand it! ;-)

                Regards, Ray

                T 1 Reply Last reply
                0
                • R Ray Hayes

                  I've now got this, but it still doesn't look like it works: Xaml is now:

                  <Slider x:Name="scaleSlider" Width="100" Minimum="0.5" Maximum="10.0" Value="{Binding Source=scale, Path=ScalingFactor, Mode=TwoWay}"/>

                  I'd changed the binding in the MultiBinding to:

                  <Binding Source="{StaticResource scale}" Path="ScalingFactor"/>

                  Error in Output window is: System.Windows.Data Error: 35 : BindingExpression path error: 'ScalingFactor' property not found on 'object' ''String' (HashCode=-528916476)'. BindingExpression:Path=ScalingFactor; DataItem='String' (HashCode=-528916476); target element is 'Slider' (Name='scaleSlider'); target property is 'Value' (type 'Double') I'm sure the error will help, once I understand it! ;-)

                  Regards, Ray

                  T Offline
                  T Offline
                  TJoe
                  wrote on last edited by
                  #11

                  The two bindings you just posted are not the same. Compare them carefully (ignoring the fact that Mode is set on one of them though).

                  Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                  R 1 Reply Last reply
                  0
                  • T TJoe

                    The two bindings you just posted are not the same. Compare them carefully (ignoring the fact that Mode is set on one of them though).

                    Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                    R Offline
                    R Offline
                    Ray Hayes
                    wrote on last edited by
                    #12

                    Ok, given that they're both working on a source of "scale" and a Path of ScalingFactor (ignoring Mode), that leaves the fact that the second isn't a StaticResource. I'm not certain how to convert between the two markup conventions (although I guess I could expand out the Slider's "Value" element). I tried this and it doesn't change anything :-(

                    <Slider x:Name="scaleSlider" Width="100" Minimum="0.5" Maximum="10.0" Value="{Binding Source={StaticResource scale}, Path=ScalingFactor, Mode=TwoWay}"/>

                    Regards, Ray

                    T 2 Replies Last reply
                    0
                    • R Ray Hayes

                      Ok, given that they're both working on a source of "scale" and a Path of ScalingFactor (ignoring Mode), that leaves the fact that the second isn't a StaticResource. I'm not certain how to convert between the two markup conventions (although I guess I could expand out the Slider's "Value" element). I tried this and it doesn't change anything :-(

                      <Slider x:Name="scaleSlider" Width="100" Minimum="0.5" Maximum="10.0" Value="{Binding Source={StaticResource scale}, Path=ScalingFactor, Mode=TwoWay}"/>

                      Regards, Ray

                      T Offline
                      T Offline
                      TJoe
                      wrote on last edited by
                      #13

                      You still get the same binding error with that change?

                      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                      1 Reply Last reply
                      0
                      • R Ray Hayes

                        Ok, given that they're both working on a source of "scale" and a Path of ScalingFactor (ignoring Mode), that leaves the fact that the second isn't a StaticResource. I'm not certain how to convert between the two markup conventions (although I guess I could expand out the Slider's "Value" element). I tried this and it doesn't change anything :-(

                        <Slider x:Name="scaleSlider" Width="100" Minimum="0.5" Maximum="10.0" Value="{Binding Source={StaticResource scale}, Path=ScalingFactor, Mode=TwoWay}"/>

                        Regards, Ray

                        T Offline
                        T Offline
                        TJoe
                        wrote on last edited by
                        #14

                        Sorry, I noticed that you did not implement INotifyPropertyChanged correct. The parameter being passed needs to be the name of the property that changed, not some random text. Otherwise the system will never know where to look for the changed.

                        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                        R U 2 Replies Last reply
                        0
                        • T TJoe

                          Sorry, I noticed that you did not implement INotifyPropertyChanged correct. The parameter being passed needs to be the name of the property that changed, not some random text. Otherwise the system will never know where to look for the changed.

                          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                          R Offline
                          R Offline
                          Ray Hayes
                          wrote on last edited by
                          #15

                          PERFECT... that was it. Thanks a lot Tom, I felt I've learned a lot with your help! Thanks again!

                          Regards, Ray

                          1 Reply Last reply
                          0
                          • T TJoe

                            Sorry, I noticed that you did not implement INotifyPropertyChanged correct. The parameter being passed needs to be the name of the property that changed, not some random text. Otherwise the system will never know where to look for the changed.

                            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

                            U Offline
                            U Offline
                            User 3671134
                            wrote on last edited by
                            #16

                            Hi, Kindly suggest me how to use a DP as ConverterParameter in Xaml. Thnx, Ritesh

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups