Click here to Skip to main content
15,924,507 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to explore the slider control. Currently I have a requirement to use the slider control as navigation control. For that I want to databind the Tick property of the slider control to a collection.

The Ticks property is a dependency property and so it should be able to accept bindings.

This is what I am doing.

I have a class myCollectionClass with a List<Object> Property

XML
public List<double> myCollection
        {
            get
            {
                List<double> myList = new List<double>();
                myList.Add(1);
                myList.Add(2);
                myList.Add(3);
                myList.Add(4);
                return myList;
            }
        }



I want to databind the ticks to myCollection

This is what I have done in XAML

XML
<Window.Resources>
        <c:myCollectionClass x:Key="CollClass"/>
    </Window.Resources>

XML
<Grid DataContext="{StaticResource CollClass}">
        <StackPanel Grid.Row="1">
            <Slider TickPlacement="BottomRight"
                    IsSnapToTickEnabled="True"
                    Ticks="{Binding myCollection}"/>
        </StackPanel>
    </Grid>


This does not work.

If I say -

XML
<Slider TickPlacement="BottomRight" 
                    IsSnapToTickEnabled="True"
                    Ticks="1,2,3,4,5"/>


it works fine, but I am trying to bind the Ticks to the collection.

Any idea how to achieve this?
Posted
Updated 23-Feb-11 0:04am
v2

1 solution

In my opinion, the Ticks takes values of type DoubleCollection.

I tried doing it manually and found that Ticks cannot take values of Type List.

So what i think is that you can replace the List<double> with DoubleCollection. Basically DoubleCollection is non-generic and takes only values of Datatype Double and also its an OrderedCollection!

So the property should be like this:
C#
public DoubleCollection myCollection
        {
            get
            {
                DoubleCollection myList = new DoubleCollection();                
                myList.Add(1);
                myList.Add(2);
                myList.Add(3);
                myList.Add(4);
                return myList;
            }
        }


Hope it helped!
 
Share this answer
 
v4
Comments
anshudutta 23-Feb-11 9:14am    
I tried it with arrayList adding 1,2,3,4 as doubles. Still no luck. Could you get it working?
Tarun.K.S 23-Feb-11 9:34am    
What i mean is, replace myCollection property's type as List<double> with DoubleCollection because Ticks is of type DoubleCollection.
Tarun.K.S 23-Feb-11 9:38am    
Check my updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900