Reputation: 32481
I have an slider control in an canvas like this:
I add a label (which is placed in a border) to the canvas. How can I bind this border's position, so that when slider value moves along it's path, the border moves correctly and show the value with it's label.
Can anyone help? Any working idea will be appreciated.
Upvotes: 0
Views: 560
Reputation: 32481
I did it using C# coding. However it may be better solutions but it works for me.
In the ValueChanged
event of the slider I write these codes.
private void year_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (this.yearInfo == null)
{
return;
}
Point position = Mouse.GetPosition(this.mapView); //mapView is the name of my canvas
this.yearInfo.Margin =
new Thickness(0, 0, position.X - this.yearInfo.ActualWidth / 2.0, 45);
}
Upvotes: 1
Reputation: 1532
You have to manage the left margin of the border containing the label, and bind it to the slider's value. To transform the slider value into an appropiate margin you'll need to create a custom converter. Keep in mind that there is no "LeftMargin" property, you'll have to create a new margin from the slider's value and pass it to the border:
In your case I recommend you to create a converter and pass some parameters: top border margin (fixed so it moves in a straight line, could be bound to the slider's position too), min and max slider value and slider's width to scale to margin coordinates. It's not trivial...
Another way is to create a new component that inherits from slider and that incorporates the label, but you won't avoid the converter that way.
Upvotes: 2