2

我已经检查了 这个样本。它向我展示了 TimeLineMarker API。现在,当我按下按钮“添加标记”时,标记被添加并且文本块显示标记,它被添加的时间。现在我想以任何形状或其他任何符号的形式将这些标记显示到滑块控件上,所以当我想直接进入标记时,我可以直接单击滑块。我怎样才能做到这一点 ?

4

1 回答 1

1

终于从MSDN得到答案,谢谢马特

XAML

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel VerticalAlignment="Center">
         <Grid>
            <Canvas x:Name="MyCanvas" Width="{Binding ElementName=MySlider, Path=ActualWidth}" Height="{Binding ElementName=MySlider, Path=ActualHeight}" Background="BlueViolet"/>
             <Slider x:Name="MySlider" Orientation="Horizontal" Width="750" Height="50" Loaded="MySlider_Loaded_1"/>
         </Grid>
         <Button Content="Set marker" Click="Button_Click_1"/>
     </StackPanel>
</Grid>

C#

int UniqueId = 0;
List<CustomTick> MyCustomTicks = new List<CustomTick>();
Thumb MySliderThumb;
Point ThumbCurrentPoint;
private void MySlider_Loaded_1(object sender, RoutedEventArgs e)
{
    FrameworkElement templateRoot = VisualTreeHelper.GetChild((Slider)sender, 0) as FrameworkElement;
    MySliderThumb = templateRoot.FindName("HorizontalThumb") as Thumb;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
    MySliderThumb.TransformToVisual(MyCanvas).TryTransform(new Point(0, 0), out ThumbCurrentPoint);
    CustomTick MyCustomTick = new CustomTick();
    MyCustomTick.X = ThumbCurrentPoint.X;
    MyCustomTick.Y = ThumbCurrentPoint.Y;
    MyCustomTick.Value = MySlider.Value;
    MyCustomTick.ID = UniqueId++;
    MyCustomTicks.Add(MyCustomTick);
    Windows.UI.Xaml.Shapes.Rectangle MyRectangle = new Windows.UI.Xaml.Shapes.Rectangle();
    MyRectangle.Name = MyCustomTick.ID.ToString();
    MyRectangle.Fill = new SolidColorBrush(Windows.UI.Colors.Red);
    MyRectangle.Width = MySliderThumb.ActualWidth;
    MyRectangle.Height = MySliderThumb.ActualHeight;
    MyRectangle.PointerPressed += MyRectangle_PointerPressed;
    MyCanvas.Children.Add(MyRectangle);
    Canvas.SetTop(MyRectangle, MyCustomTick.Y - MyRectangle.Height );
    Canvas.SetLeft(MyRectangle, MyCustomTick.X);
}
void MyRectangle_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Shapes.Rectangle MyRectangle = sender as Windows.UI.Xaml.Shapes.Rectangle;
    CustomTick MyCustomTick = new CustomTick();
    foreach (CustomTick CT in MyCustomTicks)
    {
if (CT.ID.ToString() == MyRectangle.Name)
{
    MyCustomTick = CT;
    break;
}
    }
    MySlider.Value = MyCustomTick.Value;
}

List<CustomTick> MyTicks = new System.Collections.Generic.List<CustomTick>();
private class CustomTick
{
    public double X;
    public double Y;
    public double Value;
    public int ID;
}
于 2013-04-01T10:14:54.763 回答