WPF:将滑块值数据绑定到 XAML 中的工具提示内容字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18577521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:33:49  来源:igfitidea点击:

WPF: Data Binding A Slider Value To A ToolTip Content String In XAML

wpfxamldata-bindingslidertooltip

提问by ClockEndGooner

In a C# based WPF UserControl, I have a set of Sliders to adjust the Red,Green and Blue values of a Color element; essentially the UserControl is a color picker. I have a combination of XAML and C# code behind for the ToolTipOpening Event Handler that allows me to set the individual integer value representing the Color component property value. So, if I have the slider positioned for adjusting the amount of Red in the color in the middle of the Slider, the ToolTip will display "Red: 125", as each Color component property value can range from 0 to 255. The code fragments below for both the XAML and C# work as expected:

在基于 C# 的 WPF UserControl 中,我有一组滑块来调整 Color 元素的红色、绿色和蓝色值;本质上,UserControl 是一个颜色选择器。我有一个 XAML 和 C# 代码的组合,用于 ToolTipOpening 事件处理程序,它允许我设置表示颜色组件属性值的单个整数值。因此,如果我将滑块定位用于调整滑块中间颜色中红色的量,工具提示将显示“红色:125”,因为每个颜色组件属性值的范围可以从 0 到 255。代码片段下面的 XAML 和 C# 按预期工作:

XAML:

XAML:

<UserControl x:Class="CustomColorPicker.ColorPickerUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Name="colorPicker">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="175" />
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>

        <Label HorizontalAlignment="Right">Red:</Label>
        <Slider Grid.Row="0" Grid.Column="1" Name="sliderRed" Minimum="0" Maximum="255"
                Margin="{Binding ElementName=colorPicker, Path=Padding}"
                Value="{Binding ElementName=colorPicker, Path=Red}" 
                ToolTipOpening="OnColorSliderToolTipOpening" 
                ToolTip="Red: 0" />

        <Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right">Green:</Label>
        <Slider Grid.Row="1" Grid.Column="1" Name="sliderGreen" Minimum="0" Maximum="255"
                Margin="{Binding ElementName=colorPicker, Path=Padding}"
                Value="{Binding ElementName=colorPicker, Path=Green}" 
                ToolTipOpening="OnColorSliderToolTipOpening" ToolTip="Green: 0" />

        <Label Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right">Blue:</Label>
        <Slider Grid.Row="2" Grid.Column="1" Name="sliderBlue" Minimum="0" Maximum="255"
                Margin="{Binding ElementName=colorPicker, Path=Padding}"
                Value="{Binding ElementName=colorPicker, Path=Blue}" 
                ToolTipOpening="OnColorSliderToolTipOpening" ToolTip="Blue: 0" />

        <Rectangle Grid.Column="2" Grid.RowSpan="3"
                   Margin="{Binding ElementName=colorPicker, Path=Padding}"
                   Width="85" Stroke="Black" StrokeThickness="1">
            <Rectangle.Fill>
                <SolidColorBrush Color="{Binding ElementName=colorPicker, Path=Color}" />
            </Rectangle.Fill>
        </Rectangle>    
    </Grid>
</UserControl>

C#:

C#:

    private void OnColorSliderToolTipOpening(object sender, ToolTipEventArgs e)
    {
        Slider slider = (Slider) sender;

        string colorName = "N/A";
        int colorValue = 0;

        if (slider.Name == "sliderRed")
        {
            colorName = "Red";
            colorValue = (int) Color.R;                
        }

        else if (slider.Name == "sliderGreen")
        {
            colorName = "Green";
            colorValue = (int) Color.G;
        }

        else if (slider.Name == "sliderBlue")
        {
            colorName = "Blue";
            colorValue = (int) Color.B;
        }

        string colorTip =
        string.Format("{0}: {1}", colorName, colorValue.ToString());

        slider.ToolTip = colorTip;

        return;
    }

Is there a way to eliminate the code behind and set the ToolTip contents to display the same string using the ToolTip.Contentand ToolTip.ContentStringFormatproperties in XAML only, allowing for the elimination of the ToolTipOpening event handler in the code behind in C#? I've tried the following XAML using DataBinding to format the content string of the Slider's ToolTip:

有没有办法消除隐藏的代码并将工具提示内容设置为仅使用XAML 中的ToolTip.ContentToolTip.ContentStringFormat属性显示相同的字符串,从而允许消除 C# 中隐藏的代码中的 ToolTipOpening 事件处理程序?我已经尝试使用 DataBinding 来格式化滑块工具提示的内容字符串的以下 XAML:

XAML:

XAML:

    <Slider Grid.Row="2" Grid.Column="1" Name="sliderBlue" Minimum="0" Maximum="255"
            Margin="{Binding ElementName=colorPicker, Path=Padding}"
            Value="{Binding ElementName=colorPicker, Path=Blue}">
        <Slider.ToolTip>
            <ToolTip
                Content="{Binding ElementName=colorPicker, Path=Blue}"
                ContentStringFormat="{}Blue: {0}" />
        </Slider.ToolTip>
    </Slider>

and the ToolTip Popup display comes up empty:

并且工具提示弹出窗口显示为空:

*Slider with Empty ToolTip using XAML only*

*仅使用 XAML 的带有空工具提示的滑块*

回答by LPL

Binding per ElementNamejust like RelativeSourcewith FindAncestorisn't in scope because ToolTipis a Popupwhich isn't part of the VisualTree (it creates its own). But you can use the ToolTip.PlacementTarget Propertyto get the Slidercontrol and catch the Value.

绑定 perElementName就像RelativeSourcewithFindAncestor不在范围内,因为ToolTipis aPopup不是 VisualTree 的一部分(它创建自己的)。但是您可以使用ToolTip.PlacementTarget 属性来获取Slider控件并捕获值。

<Slider.ToolTip>
    <ToolTip Content="{Binding RelativeSource={RelativeSource Self},
                               Path=PlacementTarget.Value}"
             ContentStringFormat="Blue: {0:0}" />
</Slider.ToolTip>

Please note the additional :0in ContentStringFormat="Blue: {0:0}"to round the Value.

请注意,附加:0ContentStringFormat="Blue: {0:0}"以全面价值。