gpt4 book ai didi

c# - WPF 将 slider 的鼠标位置值绑定(bind)到文本 block

转载 作者:行者123 更新时间:2023-11-30 22:13:56 25 4
gpt4 key购买 nike

我有一个 slider ,最小值为 0,最大值为 100。

当我将鼠标悬停在 slider 上时,我会看到一个弹出窗口。
目前,我将弹出窗口与 slider 值绑定(bind)。

我想要的是:
当我将鼠标移到 slider 上时,我想查看假定位于该位置的值。

例如:
enter image description here

我的 XAML:

<Window x:Class="WPFMavka.VideoPlayer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFMavka"
Title="MainWindow" WindowStyle="None" Width="1920" Height="1080" WindowState="Maximized" KeyboardNavigation.TabNavigation="None" Closing="Window_Closing">
<Grid Name="tst">
<local:MetroSlider x:Name="bla" MouseMove="Rectangle_MouseMove" MouseLeave="Rectangle_MouseLeave" IsMoveToPointEnabled="True" HorizontalAlignment="Left" Width="542" VerticalAlignment="Top" Margin="116,839,0,0" Value="0" Style="{DynamicResource SliderStyle1}" Height="66" AutoToolTipPrecision="0" AutoToolTipPlacement="TopLeft"/>
<Popup Name="floatingTip" AllowsTransparency="True" Placement="Relative" PlacementTarget="{Binding ElementName=bla}">
<Border BorderBrush="Black" BorderThickness="1" Padding="4">
<TextBlock Text="{Binding ElementName=bla, Path=Value}"></TextBlock>
</Border>
</Popup>
</Grid>
</Window>

我的代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPFMavka
{
/// <summary>
/// Interaction logic for VideoPlayer.xaml
/// </summary>
public partial class VideoPlayer : Window
{
public VideoPlayer()
{
InitializeComponent();
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Environment.Exit(1);
}

private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{
if (!floatingTip.IsOpen) { floatingTip.IsOpen = true; }

Point currentPos = e.GetPosition(bla);

floatingTip.HorizontalOffset = currentPos.X-14;
floatingTip.VerticalOffset = -32;
}

private void Rectangle_MouseLeave(object sender, MouseEventArgs e)
{
floatingTip.IsOpen = false;
}
}
}

最佳答案

找到了更好的解决方案 - 更改 TextBlock onMouseOver 幻灯片 > 我得到鼠标在 slider 上的当前位置,然后使用 .ValueFromPoint()

XAML:

<Window x:Class="WPFMavka.VideoPlayer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFMavka"
Title="MainWindow" WindowStyle="None" Width="1920" Height="1080" WindowState="Maximized" KeyboardNavigation.TabNavigation="None" Closing="Window_Closing">
<Grid Name="tst">
<local:MetroSlider x:Name="slider" MouseMove="slider_MouseMove" MouseLeave="slider_MouseLeave" IsMoveToPointEnabled="True" HorizontalAlignment="Left" Width="746" VerticalAlignment="Top" Margin="330,851,0,0" Value="0" Style="{DynamicResource SliderStyle1}" Height="44"/>
<Popup Name="floatingTip" AllowsTransparency="True" Placement="Relative" PlacementTarget="{Binding ElementName=slider}">
<Border Name="floatingTipBorder" BorderBrush="Black" BorderThickness="1" Padding="4">
<TextBlock Name="sliderTextBlock"/>
</Border>
</Popup>
</Grid>
</Window>

代码隐藏:

private void slider_MouseMove(object sender, MouseEventArgs e)
{
if (!floatingTip.IsOpen) { floatingTip.IsOpen = true; }

Point currentPos = e.GetPosition(slider);
Track _track = slider.Template.FindName("PART_Track", slider) as Track;

sliderTextBlock.Text = _track.ValueFromPoint(currentPos).ToString();

floatingTip.HorizontalOffset = currentPos.X -(floatingTipBorder.ActualWidth / 2);
floatingTip.VerticalOffset = -32;
}

private void slider_MouseLeave(object sender, MouseEventArgs e)
{
floatingTip.IsOpen = false;
}

关于c# - WPF 将 slider 的鼠标位置值绑定(bind)到文本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18759127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com