How to make and implement custom tooltip with attached property of Title and Description in WinUI-3 C#?

huangapple go评论61阅读模式
英文:

How to make and implement custom tooltip with attached property of Title and Description in WinUI-3 C#?

问题

custom class name - CustomToolTip

Attached properties - Title and Description

I expecting like in WinUI-3 MainWindow.xaml:

    <ribbon:RibbonGroup Header="Home">
        <ribbon:RibbonButton x:Name="radRibbonGroupClipboard"
                            ribbon:ScreenTip.Description="Show the Clipboard Task options."
                            ribbon:ScreenTip.Title="Clipboard" />
    </ribbon:RibbonGroup>
</ribbon:RibbonTab>
英文:

custom class name - CustomToolTip

Attached properties - Title and Description

I expecting like in WinUI-3 MainWindow.xaml:

 &lt;ribbon:RibbonTab Header=&quot;radRibbonView&quot;&gt; 
    &lt;ribbon:RibbonGroup Header=&quot;Home&quot;&gt; 
        &lt;ribbon:RibbonButton x:Name=&quot;radRibbonGroupClipboard&quot;  
                                ribbon:ScreenTip.Description=&quot;Show the Clipboard Task options.&quot; 
                                ribbon:ScreenTip.Title=&quot;Clipboard&quot; /&gt; 
    &lt;/ribbon:RibbonGroup  &gt; 
 &lt;/ribbon:RibbonTab &gt;
`


</details>


# 答案1
**得分**: 0

如果您想要类似 Telerik ScreenTip 的东西,您应该尝试使用内置的 "ToolTip"。

```xaml
<Button
    Content="点击"
    ToolTipService.Placement="底部">
    <ToolTipService.ToolTip>
        <Grid RowDefinitions="Auto,*">
            <TextBlock
                Grid.Row="0"
                FontWeight="SemiBold"
                Text="一些标题" />
            <TextBlock
                Grid.Row="1"
                Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
                TextWrapping="Wrap" />
        </Grid>
    </ToolTipService.ToolTip>
</Button>
英文:

If you want something similar to the Telerik ScreenTip, you should try the built-in ToolTip.

&lt;Button
    Content=&quot;Click&quot;
    ToolTipService.Placement=&quot;Bottom&quot;&gt;
    &lt;ToolTipService.ToolTip&gt;
        &lt;Grid RowDefinitions=&quot;Auto,*&quot;&gt;
            &lt;TextBlock
                Grid.Row=&quot;0&quot;
                FontWeight=&quot;SemiBold&quot;
                Text=&quot;Some Title&quot; /&gt;
            &lt;TextBlock
                Grid.Row=&quot;1&quot;
                Text=&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&quot;
                TextWrapping=&quot;Wrap&quot; /&gt;
        &lt;/Grid&gt;
    &lt;/ToolTipService.ToolTip&gt;
&lt;/Button&gt;

答案2

得分: 0

以下是代码的翻译部分:

ScreenTip.cs

using Microsoft.UI.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;

namespace TipTests;

public class ScreenTip
{
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.RegisterAttached(
            "Title",
            typeof(string),
            typeof(ScreenTip),
            new PropertyMetadata(string.Empty, OnTitlePropertyChanged));

    public static readonly DependencyProperty DescriptionProperty =
        DependencyProperty.RegisterAttached(
            "Description",
            typeof(string),
            typeof(ScreenTip),
            new PropertyMetadata(string.Empty, OnDescriptionPropertyChanged));

    public static readonly DependencyProperty PlacementProperty =
        DependencyProperty.RegisterAttached(
            "Placement",
            typeof(PlacementMode),
            typeof(ScreenTip),
            new PropertyMetadata(default, OnPlacementPropertyChanged));

    public static string GetTitle(DependencyObject obj) => (string)obj.GetValue(TitleProperty);

    public static void SetTitle(DependencyObject obj, string value) => obj.SetValue(TitleProperty, value);

    public static string GetDescription(DependencyObject obj) => (string)obj.GetValue(DescriptionProperty);

    public static void SetDescription(DependencyObject obj, string value) => obj.SetValue(DescriptionProperty, value);

    public static PlacementMode GetPlacement(DependencyObject obj) => (PlacementMode)obj.GetValue(PlacementProperty);

    public static void SetPlacement(DependencyObject obj, PlacementMode value) => obj.SetValue(PlacementProperty, value);

    private static void OnTitlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Tip tip = GetTip(d);
        tip.TitleTextBlock.Text = e.NewValue as string ?? string.Empty;
        tip.TitleTextBlock.FontWeight = FontWeights.SemiBold;

        // 获取父级。
        if (d is Button parent)
        {
            // 做一些操作。
        }
    }

    private static void OnDescriptionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Tip tip = GetTip(d);
        tip.DescriptionTextBlock.Text = e.NewValue as string ?? string.Empty;
        tip.DescriptionTextBlock.TextWrapping = TextWrapping.Wrap;
    }

    private static void OnPlacementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue is PlacementMode placement)
        {
            ToolTipService.SetPlacement(d, placement);
        }
    }

    private static Tip GetTip(DependencyObject dependencyObject)
    {
        if (ToolTipService.GetToolTip(dependencyObject) is not Tip tip)
        {
            tip = new Tip();
            ToolTipService.SetToolTip(dependencyObject, tip);
        }

        return tip;
    }

    private class Tip : Grid
    {
        public Tip()
        {
            this.RowDefinitions.Add(new RowDefinition
            {
                Height = new GridLength(0, GridUnitType.Auto)
            });

            Grid.SetRow(this.TitleTextBlock, 0);
            this.Children.Add(this.TitleTextBlock);

            this.RowDefinitions.Add(new RowDefinition
            {
                Height = new GridLength(0, GridUnitType.Star)
            });

            Grid.SetRow(this.DescriptionTextBlock, 1);
            this.Children.Add(thisDescriptionTextBlock);
        }

        public TextBlock TitleTextBlock { get; } = new();

        public TextBlock DescriptionTextBlock { get; } = new();
    }
}
英文:

Here's another option using an AttachedProperty.

&lt;Window
x:Class=&quot;TipTests.MainWindow&quot;
xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
xmlns:local=&quot;using:TipTests&quot;
xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
mc:Ignorable=&quot;d&quot;&gt;
&lt;Grid&gt;
&lt;Button
local:ScreenTip.Description=&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&quot;
local:ScreenTip.Placement=&quot;Bottom&quot;
local:ScreenTip.Title=&quot;LOREM IPSUM&quot;
Content=&quot;Click&quot; /&gt;
&lt;/Grid&gt;
&lt;/Window&gt;

ScreenTip.cs

using Microsoft.UI.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;

namespace TipTests;

public class ScreenTip
{
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.RegisterAttached(
            &quot;Title&quot;,
            typeof(string),
            typeof(ScreenTip),
            new PropertyMetadata(string.Empty, OnTitlePropertyChanged));

    public static readonly DependencyProperty DescriptionProperty =
        DependencyProperty.RegisterAttached(
            &quot;Description&quot;,
            typeof(string),
            typeof(ScreenTip),
            new PropertyMetadata(string.Empty, OnDescriptionPropertyChanged));

    public static readonly DependencyProperty PlacementProperty =
        DependencyProperty.RegisterAttached(
            &quot;Placement&quot;,
            typeof(PlacementMode),
            typeof(ScreenTip),
            new PropertyMetadata(default, OnPlacementPropertyChanged));

    public static string GetTitle(DependencyObject obj) =&gt; (string)obj.GetValue(TitleProperty);

    public static void SetTitle(DependencyObject obj, string value) =&gt; obj.SetValue(TitleProperty, value);

    public static string GetDescription(DependencyObject obj) =&gt; (string)obj.GetValue(DescriptionProperty);

    public static void SetDescription(DependencyObject obj, string value) =&gt; obj.SetValue(DescriptionProperty, value);

    public static PlacementMode GetPlacement(DependencyObject obj) =&gt; (PlacementMode)obj.GetValue(PlacementProperty);

    public static void SetPlacement(DependencyObject obj, PlacementMode value) =&gt; obj.SetValue(PlacementProperty, value);

    private static void OnTitlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Tip tip = GetTip(d);
        tip.TitleTextBlock.Text = e.NewValue as string ?? string.Empty;
        tip.TitleTextBlock.FontWeight = FontWeights.SemiBold;

        // Getting the parent.
        if (d is Button parent)
        {
            // Do something.
        }
    }

    private static void OnDescriptionPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Tip tip = GetTip(d);
        tip.DescriptionTextBlock.Text = e.NewValue as string ?? string.Empty;
        tip.DescriptionTextBlock.TextWrapping = TextWrapping.Wrap;
    }

    private static void OnPlacementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue is PlacementMode placement)
        {
            ToolTipService.SetPlacement(d, placement);
        }
    }

    private static Tip GetTip(DependencyObject dependencyObject)
    {
        if (ToolTipService.GetToolTip(dependencyObject) is not Tip tip)
        {
            tip = new Tip();
            ToolTipService.SetToolTip(dependencyObject, tip);
        }

        return tip;
    }

    private class Tip : Grid
    {
        public Tip()
        {
            this.RowDefinitions.Add(new RowDefinition
            {
                Height = new GridLength(0, GridUnitType.Auto)
            });

            Grid.SetRow(this.TitleTextBlock, 0);
            this.Children.Add(this.TitleTextBlock);

            this.RowDefinitions.Add(new RowDefinition
            {
                Height = new GridLength(0, GridUnitType.Star)
            });

            Grid.SetRow(this.DescriptionTextBlock, 1);
            this.Children.Add(this.DescriptionTextBlock);
        }

        public TextBlock TitleTextBlock { get; } = new();

        public TextBlock DescriptionTextBlock { get; } = new();
    }
}

huangapple
  • 本文由 发表于 2023年3月3日 19:21:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定