views:

294

answers:

1

I currently have a canvas with an image background in a WPF application. Above this canvas, I have a slider control. I need to, as users slide the value of the slider back and forth, draw a red line straight down across the canvas. I need to do this every time the slider value is changed such that the red line is always aligned with the slider's thumb. The big problem I'm having here is trying to figure out how to efficiently draw the line, and then "erase" the previously drawn line and drawing a new line at the new thumb value when user's change the slider's value. If I simply redraw the canvas's background image, the application lags a lot and doesn't work well (plus, this just straight out doesn't completely solve the problem as you can still see the previously drawn lines anyway).

Any help would be absolutely appreciated, particularly with examples as I this is my first WPF application in C# and I'm stilling getting a feel for it's uniqueness (as opposed to Windows Forms). Thanks a lot!

+1  A: 

Rather than trying to draw the line yourself, a more WPF way to approach it would be to use a Line object. See Shapes and Basic Drawing in WPF Overview. Since you want the line to be aligned with the Slider, you can use data binding to bind the X position of the line to the position of the Slider:

<DockPanel>
    <Slider
        Name="HorizontalSlider"
        DockPanel.Dock="Top"
        Maximum="{Binding ActualWidth, ElementName=Canvas}"/>
    <Canvas Name="Canvas" Margin="5.5 0">
        <Line
            X1="{Binding Value, ElementName=HorizontalSlider}"
            Y1="0"
            X2="{Binding Value, ElementName=HorizontalSlider}"
            Y2="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType=Canvas}}"
            Stroke="Red"/>
    </Canvas>
</DockPanel>

(Note that I cheated a little by setting the margin on the Canvas to 5.5. The thumb on the slider has some thickness and doesn't move the entire width of the slider control, so in order to get the line to line up with the center I tried to make the canvas have the same width as the track.)

Quartermeister
Awesome! I've never used binding like this in WPF; it looks very useful and seems to work marvelously! However, I have one more question for you. Is there any way to possibly bind X1 to something along the lines of "Value * 3" rather than just 'Value'? My image that's the background is quite large, so I have it scaled down to fit the canvas, however, I'd need the line to be drawn, not at the slider.Value value, but rather at that value times 3 (since I have the Maximum of the slider set to 3000, but the image is only 1000 pixels long)? Thanks again!
JToland
@JToland: To do something like that in general, you will need to put a converter on the binding. Read the documentation for IValueConverter for more information. In this case, you probably just want to bind the maximum value of the slider to the width of your image instead of the width of the canvas so that the maximum is only 1000.
Quartermeister