2
我已檢查this示例。它向我展示了TimeLineMarker API。現在,當我按下「添加標記」按鈕時,添加了標記並且文本塊顯示了添加標記的標記。現在我想以任何形狀或任何形式的任何符號的形式將這些標記顯示到Slider控件上,所以當我想直接進入標記時,我可以直接單擊滑塊。我怎樣才能做到這一點 ?如何將書籤放到滑塊上,同時在c#/ xaml metro app中添加時間軸標記
我已檢查this示例。它向我展示了TimeLineMarker API。現在,當我按下「添加標記」按鈕時,添加了標記並且文本塊顯示了添加標記的標記。現在我想以任何形狀或任何形式的任何符號的形式將這些標記顯示到Slider控件上,所以當我想直接進入標記時,我可以直接單擊滑塊。我怎樣才能做到這一點 ?如何將書籤放到滑塊上,同時在c#/ xaml metro app中添加時間軸標記
最後得到的答案從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;
}