Bind Scale to Slider

I'm dabbling with Avalon and I've come across a problem. As a test, I want to bind the scale of a TreeView to a Slider, which has a Minimum and Maximum of 1 and 10, respectively.

Now, I found out that I could not bind via the LayoutTransform property on the TreeView itself ("scale {Binding ElementName=slider, Path=Value}" does not compile). Instead, I tried this:

<TreeView.LayoutTransform>
   <
ScaleTransform ScaleX="{Binding ElementName=slider, Path=Value}" />
</
TreeView.LayoutTransform>

This time, it was complaining that ScaleX needed a double value, which it was not getting. So I looked into creating a Converter. I made a simple class:


public class Test : IValueConverter

{

#region IValueConverter Members

object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

return Double.Parse(value);

}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

return value.ToString();

}

#endregion

}


 



Then added this to the top of my XAML:

< Mapping XmlNamespace="T" ClrNamespace="AvalonApplication5" >

Added this to my Window:

xmlns:T="T"

And this into my TreeView:

<TreeView.Resources>

<T:Test x:Key="Conv" />

</TreeView.Resources>

And changed my ScaleX to this:

ScaleX="{Binding ElementName=slider, Path=Value, Converter={StaticResource Conv}}"

Now, when I try to run, it tells me:
Cannot locate the resource 'window1.baml'.

Does anyone know of an easier way to do this

Thanks,
Johann



Answer this question

Bind Scale to Slider

  • Allen Clark MSFT

    Thanks! That fixed the baml file problem. Now, however, whenever I run I get the error:

    Value cannot be null.
    Parameter name: d

    I've set breakpoints on my IValueConverter but they never get called.


  • GPAustralia

    There are some issues binding to Freezables in September CTP, and you've just run into one of them. This has been fixed in more recent bits though.



  • HgEuAaVrEdN

    Almost there! Now I get this:

    Object of type 'System.Object' cannot be converted to type 'System.ComponentModel.IValueConverter'.

    There's also another problem. In my XAML, my line:
    <T:Test x:Key="Conv" />
    Has a blue line underneath and it warning me:

    The element 'TreeView.Resources' in namespace 'http://schemas.microsoft.com/winfx/avalon/2005' has invalid child element 'Test' in namespace 'T'. Expected 'ResourceDictionary.MergedDictionaries, ResourceDictionary.Keys, ResourceDictionary.Values' in namespace 'http://schemas.microsoft.com/winfx/avalon/2005' as well as 'Code' in namespace 'http://schemas.microsoft.com/winfx/xaml/2005' as well as 'Point, Size, Vector, Rect, Int32Rect, FontStyle, FontStretch, FontWeight, Duration, KeyTime, RepeatBehavior, Color, PixelFormat, Matrix3D, Point3D, Point4D, Quaternion, Rect3D, Size3D, Vector3D, CornerRadius, FigureLength, GridLength, Thickness, DependencyProperty, DependencyObject, TextDecoration, TextDecorationCollection, DoubleAnimationUsingPath, DoubleAnimation, DoubleAnimationUsingKeyFrames, MatrixAnimationUsingPath, MatrixAnimationUsingKeyFrames, PointAnimationUsingPath, PointAnimation, PointAnimationUsingKeyFrames, ByteAnimation, ByteAnimationUsingKeyFrames, ColorAnimation, ColorAnimationUsingKeyFrames, DecimalAnimation, DecimalAnimationUsingKeyFrames, Int16Animation, Int16AnimationUsingKeyFrames, Int32Animation, Int32AnimationUsingKeyFrames, Int64Animation, I....

    Am I defining my Converter correctly   


  • GERT86

    To implement Fil's (excellent!!) workaround, you could do the following:

    <Window.Resources>
       (...)
       
    <src:DoubleToScaleTransformConverter x:Key="converter"/>
    </
    Window.Resources>

    <TreeView LayoutTransform="{Binding ElementName=slider, Path=Value, Converter={StaticResource converter}}" (...)>(...)</TreeView>

    <Slider Value="1" Minimum="1" Maximum="10" Name="slider"/>


    The item passed as a parameter to the Convert method in the converter is of type double because you are binding to the slider's Value (which is of type double). The object returned from the Converter method should be the ScaleTransform because that's the object you want to assign to the LayoutTransform property of TreeView.

    public class DoubleToScaleTransformConverter : IValueConverter
    {

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
       
    ScaleTransform transform = new ScaleTransform();
       transform.ScaleX = (
    double)value;
       
    return transform;
    }

    (...)

    }

    I made a little sample for you to play with, which you can find here:
    http://www.beacosta.com/Forum/BindScaleTransformToSlider.zip

    Let us know if this helped and if you have any further questions.

    Bea



  • cyberkid1043

    If you need a workaround until the next CTP, what you can do is bind the LayoutTransform property instead of trying to bind the ScaleX property of the Transform.

    The markup would look something like this:

    <TreeView LayoutTransform="{Binding ElementName=slider, Path=Value, Converter={StaticResource Conv}}" />

    Make sure your value converter returns a ScaleTransform instead of a Double now.


  • xfiles

    I ran into this problem once before, try closing VS and building clean. Should work.


  • Bind Scale to Slider