Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The scrollbar I want is like this ->

http://www.songpengjie.cn/wp-content/uploads/2014/08/Pic.jpg[^]

I have defined a ListView to show some items. I actually want to show the name of first item which shown in viewport in the tip.

Thank you.
Posted

Just to get you on your way:
This example works only when the items have the same height.
If not, you have to make a list of all Y positions and get the text from that list.

XML
<Window x:Class="WpfApplicationtest2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" SizeChanged="Window_SizeChanged">
    <Grid>
        <Canvas>
            <Border Name="BorderPageNumber" BorderBrush="Black" removed="#66454545">
                <TextBlock Name="TextBoxTest" FontSize="22" Width="150" Height="30">Test</TextBlock>
            </Border>
        </Canvas>
        <ScrollViewer Name="ScrollviewerTest" Loaded="ScrollviewerTest_Loaded" ScrollChanged="ScrollviewerTest_ScrollChanged">
            <StackPanel Name="StackPanelScrollItems" Orientation="Vertical">
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 1</TextBlock>
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 2</TextBlock>
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 3</TextBlock>
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 4</TextBlock>
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 5</TextBlock>
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 6</TextBlock>
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 7</TextBlock>
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 8</TextBlock>
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 9</TextBlock>
                <TextBlock Margin="10,10,0,50" FontSize="22">Test 10</TextBlock>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Window>


C#
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        List<point> ContentPositions = new List<point>();
        private string GetItemText(int index)
        {
            return ((TextBlock)StackPanelScrollItems.Children[index]).Text;
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ScrollviewerTest.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            ((TextBlock)StackPanelScrollItems.Children[0]).Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            SetInfoPosition();
        }
        private void SetInfoPosition()
        {
            BorderPageNumber.Margin = new Thickness(ScrollviewerTest.ActualWidth - 160, (ScrollviewerTest.ViewportHeight / 2.0) - 30, 0, 0);
        }

        private void ScrollviewerTest_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            int pageNumber = e.VerticalOffset==0 ? 1 : ((int)(e.VerticalOffset / e.ViewportHeight) + 1);
            int itemIndex = e.VerticalOffset == 0 ? 1 : (int)(e.VerticalOffset / (((TextBlock)StackPanelScrollItems.Children[0]).DesiredSize.Height)) + 1;
            TextBoxTest.Text = pageNumber.ToString() + "  " + GetItemText(itemIndex);
        }

        private void ScrollviewerTest_Loaded(object sender, RoutedEventArgs e)
        {
            SetInfoPosition();
        }
    }
</point></point>
 
Share this answer
 
v2
Thanks, Jan Bakker! I have solved this myself. The below is my code:
XML
<style x:key="ScrollBarThumb" targettype="{x:Type Thumb}" mode="hold" xmlns:x="#unknown" />            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="IsTabStop" Value="false"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
                        <Grid HorizontalAlignment="Right">
                            <Rectangle x:Name="thumb" Fill="CornflowerBlue" RadiusX="0" RadiusY="0" Width="10"/>
                            <Popup x:Name="popup" PopupAnimation="Fade" Placement="Left" PlacementTarget="{Binding ElementName=thumb}" AllowsTransparency="True" HorizontalOffset="-16">
                                <Grid>
                                    <Rectangle Fill="DodgerBlue" RadiusX="5" RadiusY="5" Opacity="0.5" Margin="0,0,6,0">
                                    </Rectangle>
                                    <Path Data="M16,10L22,13 16,16" VerticalAlignment="Top" HorizontalAlignment="Right" Fill="DodgerBlue" Opacity="0.5"></Path>
                                    <TextBlock x:Name="tooltip" Padding="10,5,16,5" VerticalAlignment="Center" FontSize="12" Foreground="White"/>
                                    <Grid.Effect>
                                        <DropShadowEffect ShadowDepth="0" BlurRadius="2" Color="Black"/>
                                    </Grid.Effect>
                                </Grid>
                            </Popup>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="popup" Property="IsOpen" Value="True"/>
                                <Setter TargetName="thumb" Property="Fill" Value="DodgerBlue"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


C#
private void AdjustPositionOfThunbTip(Thumb myThumb)
        {
            var ThumbTemplate = myThumb.Template;
            Popup popup = (Popup)ThumbTemplate.FindName("popup", myThumb);
            TextBlock tooltip = (TextBlock)ThumbTemplate.FindName("tooltip", myThumb);
            double VerticalOffset = (myThumb.ActualHeight - tooltip.FontSize - tooltip.Padding.Top - tooltip.Padding.Bottom) / 2;
            popup.VerticalOffset = VerticalOffset;
            double HorizontalOffset = popup.HorizontalOffset;
            popup.HorizontalOffset = HorizontalOffset + 0.0000001;
            popup.HorizontalOffset = HorizontalOffset;
            ScrollViewer sv = FindVisualAncestor<scrollviewer>(myThumb);
            if (sv.CanContentScroll == true)
            {
                VirtualizingStackPanel panel = FindVisualChild<virtualizingstackpanel>(this.AccountsView);
                if (this.AccountsView.Items.Count > 0 && panel != null)
                {
                    int index =
                      (panel.Orientation == Orientation.Horizontal) ? (int)panel.HorizontalOffset : (int)panel.VerticalOffset;
                    var item = this.AccountsView.Items[index];
                    Account acc = item as Account;
                    tooltip.Text = acc.Name;
                }
            }
            else
            {
                HitTestResult hitTest = VisualTreeHelper.HitTest(this.AccountsView, new Point(5, 5));
                int index = GetFirstVisibleIndexFromListbox(this.AccountsView);
                var item = this.AccountsView.Items[index];
                Account acc = item as Account;
                tooltip.Text = acc.Name;
            }
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900