捕获onMouseClick上的进度条值

时间:2013-12-29 09:02:49

标签: c# wpf xaml

我有进度条,当我点击进度条时,它应该获得值(单击鼠标的进度条) 这是进度条的XAML

<ProgressBar x:Name="seekBar" Height="21" Margin="30,36,43,0" VerticalAlignment="Top" ValueChanged="seekBar_ValueChanged" BorderBrush="#FF9B99C3" BorderThickness="2">
    <ProgressBar.Foreground>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF505285" Offset="0"/>
            <GradientStop Color="#FF858AF0" Offset="1"/>
        </LinearGradientBrush>
    </ProgressBar.Foreground>
    <ProgressBar.Background>
        <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
            <GradientStop Color="White" Offset="1"/>
            <GradientStop Color="White"/>
            <GradientStop Color="#FF0F0F0F" Offset="1"/>
            <GradientStop Color="#FF326ED6" Offset="1"/>
            <GradientStop Color="#FFD1DBEE" Offset="0.491"/>
        </LinearGradientBrush>
    </ProgressBar.Background>
    <ProgressBar.Effect>
        <DropShadowEffect Opacity="0.15"/>
    </ProgressBar.Effect>
</ProgressBar>

当值改变时,它调用函数C#代码

private void seekBar_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    this.seekBar.Value = //what should i do here??
    //how to get the value of progressbar(where mouse is clicked)
}

1 个答案:

答案 0 :(得分:5)

试试这个:

private void seekBar_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    double MousePosition = e.GetPosition(seekBar).X;
    this.seekBar.Value = SetProgressBarValue(MousePosition);
}

private double SetProgressBarValue(double MousePosition)
{
    double ratio = MousePosition/seekBar.ActualWidth;
    double ProgressBarValue = ratio*seekBar.Maximum;
    return ProgressBarValue;
}

根据以下参考编制的代码(参见讨论结尾处发布的代码):http://bytes.com/topic/c-sharp/answers/722714-allowing-user-click-progressbar