Condividi tramite


Slider control styling tips

JJ170228.0b2c798e-86c6-4ea2-a8a6-5cf1f12be1e2(it-it,VS.110).png

You can use the built-in Slider control template to create a custom Slider template. By default, the Slider control template looks like the following:

JJ170228.42dd6b3d-3876-435a-b401-1f9d2c815454(it-it,VS.110).png

Parts of the Slider template

The Slider template includes two sets of parts: one set for a vertical slider, and one set for a horizontal slider. The part names are prefixed by the word "vertical" or "horizontal," depending on the orientation of the slider.

SuggerimentoSuggerimento

To view the parts of the template, open the Parts panel while modifying the template.

JJ170228.7db0aaf3-dcb4-427a-9ebc-3d192e34df54(it-it,VS.110).png

Part name

Object type

JJ170228.25182a96-9a69-478a-9cfe-5b360e6a9bea(it-it,VS.110).png HorizontalTemplate

JJ170228.05fe4b1d-f584-4bd2-99b6-8bc2d735a89d(it-it,VS.110).png VerticalTemplate

FrameworkElement

JJ170228.f0c1ff71-7814-42ba-806b-7ea92d616e69(it-it,VS.110).png HorizontalTrackLargeChangeDecreaseRepeatButton

JJ170228.eb6fad93-f17e-4f62-a926-8c8651862891(it-it,VS.110).png VerticalTrackLargeChangeDecreaseRepeatButton

RepeatButton

JJ170228.a5d608f2-bba2-48c5-8b15-2c115db86acc(it-it,VS.110).png HorizontalThumb

JJ170228.15de085f-48f5-41dd-a286-e3dcb4cfd18b(it-it,VS.110).png VerticalThumb

Thumb

JJ170228.1d2fe38a-3fd8-4951-b978-7e44b6f2401d(it-it,VS.110).png HorizontalTrackLargeChangeIncreaseRepeatButton

JJ170228.1aa736bd-cd0d-4514-a5e4-b495d11d4870(it-it,VS.110).png VerticalTrackLargeChangeIncreaseRepeatButton

RepeatButton

Slider tips

JJ170228.collapse_all(it-it,VS.110).gifTemplate part

This part is mandatory. The template part contains all the elements that make up the Slider in a particular orientation.

To make a template part, create a grid named HorizontalTemplate or VerticalTemplate that contains three columns (for horizontal orientation) or rows (for vertical orientation) that are sized Auto, Auto, and Star.

JJ170228.collapse_all(it-it,VS.110).gifThumb part

This part is mandatory. The thumb is the movable element in the slider.

To make the object or objects that represent the thumb into a Thumb control (HorizontalThumb or VerticalThumb), place the Thumb part in the middle column (or row) of the Root part. Then, set the Width and Height properties of the Thumb part so that the column (or row) will be resized to fit the thumb.

JJ170228.collapse_all(it-it,VS.110).gifTrack

The space in which the thumb moves is called the track. The track is not a part and is optional.

To include a track, place the object or objects representing the track into the Template part, spanning all three columns (or rows).

JJ170228.collapse_all(it-it,VS.110).gifMoving the thumb along the track in large increments

If you want the user to be able to click the track on either side of the thumb to move the thumb in large increments, place a RepeatButton control in the two columns (or rows) on either side of the Thumb part, and name them VerticalLargeDecrease and VerticalLargeIncrease (or HorizontalLargeDecrease and HorizontalLargeIncrease). To make the RepeatButton controls function but not be seen, give them zero Opacity. Alternatively, apply a template that contains a single object with zero Opacity to the RepeatButton controls.

States of the Slider control

By default, the Slider control can be in one of the following three states in the CommonStates state group, which you can view in the States panel when you modify a Slider template:

State name

Description

Normal

The appearance of the Slider control when there is no interaction with the control.

MouseOver

The appearance of the Slider control when the user moves the pointer over it.

Disabled

The appearance of the Slider control when the IsEnabled property is set to False.

The Slider control can be in one of the following two states of the FocusStates state group:

State name

Description

Unfocused

The appearance of the Slider control when it does not have keyboard focus.

Focused

The appearance of the Slider control when it has keyboard focus. For example, a user might press the Tab key to cycle through the objects in your application until keyboard focus is on the Slider control.

SuggerimentoSuggerimento

A state group contains the visual states that are part of the same logical category, and that cannot be displayed at the same time. For example, the CommonStates group includes states that relate to user interaction with an input device such as the mouse. Only one state in a state group can be displayed at a time, but a state from one group can be displayed at the same time as a state in another state group.

When you select a state, state recording is turned on, and any changes that you make will be recorded for that state. To turn off state recording, click the recording button JJ170228.0dcc6415-6d4e-4fcf-a9a3-eb4664cb6cbc(it-it,VS.110).png, or select Base in the States panel. To modify the appearance of your control when two separate states are active, you can pin a preview of a state in one state group while you modify a state in a different state group.

Template-binding

You can template-bind the Background, BorderBrush, Foreground, BorderThickness, or Padding properties. For more information, see Carry object properties through to the template.

To convert objects into a Slider control

The following image is a design comprehensive (comp) of a slider created by a designer that includes MouseOver and Pressed states of the HorizontalThumb part.

Normal

Slider nello stato Normal

MouseOver

Slider nello stato MouseOver

Pressed

Slider nello stato Pressed

Disabled

Slider nello stato Disabled

This example uses the XAML code in step 2 of the following procedure, which corresponds to the preceding graphics to create a custom scroll bar by using the Slider control template.

SuggerimentoSuggerimento

Click Split View to see the changes in both Design view and Code view as you follow this procedure.

  1. In Code view, locate the following code, and then delete the closing slash mark (/).

    <Grid x:Name="LayoutRoot" Background="White"/>
    
  2. Copy and then paste the following code into your new project, after the code that you located in step 1.

    <Grid Width="146" Height="17" >
    <Rectangle x:Name="track" Fill="#FF3D3D3D" StrokeThickness="0" Margin="0,6" />
    <Rectangle x:Name="thumb" Fill="Gray" RadiusX="3" RadiusY="3" Width="7"/>
    </Grid>
    
  3. Add a closing Grid tag (</Grid>) after the code that you just pasted.

  4. In the Objects and Timeline panel, right-click Grid, and then click Make Into Control. In the Make Into Control dialog box, click Slider, and then click OK.

    In Code view, note that the code that you pasted in the preceding step has been replaced with the code for a new Slider. In addition, Blend turned Grid into a template for a new Slider style and applied that template to Grid.

  5. To add columns to the Template part grid, in Design view, click and drag the blue ruler to the left of the control to add new rows, as illustrated in the following image:

    If you add the rows as shown, the parts will be in the correct rows. Track has a ColumnSpan of 3, and thumb is in Column 1.

    [!NOTA]

    Numbering begins at 0, so the 3 columns are numbered 0 through 2.

  6. To make the root of the template into the HorizontalTemplate part, in the Objects and Timeline panel, right-click Grid, click Make Into Part of Slider, and then click HorizontalTemplate. Note that Grid has been renamed HorizontalTemplate.

  7. In the Objects and Timeline panel, right-click thumb, click Make Into Part of Slider, and then click HorizontalThumb.

  8. To add states to the HorizontalThumb part, do the following:

    1. In the Objects and Timeline panel, click thumb.

    2. In the States panel, click MouseOver.

    3. In the Properties panel, set the Fill to #FFCCCCCC.

    4. Return to the States panel, and then click Base to end state recording.

      [!NOTA]

      You can also end state recording by clicking the Record mode indicator JJ170228.0dcc6415-6d4e-4fcf-a9a3-eb4664cb6cbc(it-it,VS.110).png in the upper-left corner of the document window.

  9. Right-click MouseOver in the States panel, click Copy State To, and then click Pressed.

  10. To create the offset, with the Pressed state selected in the States panel, in the Transform category in the Properties panel, set X and Y to 1. Click Base to end state recording.

  11. In the States panel, click Normal. Click Add transition, and then click Normal to MouseOver. Set Transition duration to .2. Click Base to end state recording.

  12. Go back to template-editing mode for the Slider control [SliderStyle1 (Slider Template)] by clicking Return scope to JJ170228.55844eb3-ed98-4f20-aa66-a6f5b23eeb2b(it-it,VS.110).png in the Objects and Timeline panel.

  13. In the Objects and Timeline panel, click HorizontalThumb. In the Layout category of the Properties panel, click Advanced options next to Margin, and then click Reset.

  14. In the Objects and Timeline panel, click HorizontalTemplate. In the Parts panel, double-click HorizontalTrackLargeChangeDecreaseRepeatButton to create that part and to make it a child of HorizontalTemplate. In the Appearance category of the Properties panel, set the Opacity to 0.

  15. In the Objects and Timeline panel, click HorizontalTemplate. In the Parts panel, double-click HorizontalTrackLargeChangeIncreaseRepeatButton to create that part and to make it a child of HorizontalTemplate. In the Appearance category of the Properties panel, set the Opacity to 0.

  16. For columns 0 and 1, click the Star icon JJ170228.1b4edaf6-b6a8-4498-80dc-949375fa610d(it-it,VS.110).png two times until the Auto icon JJ170228.aa9ec064-22f8-4b62-9eed-3f4772362d22(it-it,VS.110).png appears. For column 2, make sure that the Star icon appears. Click the middle column's divider, and then, in the Properties panel, click Show advanced properties. To the right of the MinHeight value, click Advanced options, and then click Reset.

  17. In the Objects and Timeline panel, click HorizontalTemplate. In the States panel, click Disabled. In the Appearance category of the Properties panel, set Opacity to 50.

  18. Build your project by pressing Ctrl+Shift+B. When the build is complete, run your project by pressing F5, and then test your scroll bar.

References

You can find detailed information about the properties and events of the Silverlight Slider control at the Silverlight Control Gallery on MSDN.

Vedere anche

Attività

Bind an object to user input or to other internal values

Concetti

Styling tips for common Silverlight controls

SimpleSlider