英文:
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:
<ribbon:RibbonTab Header="radRibbonView">
<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 >
`
</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
.
<Button
Content="Click"
ToolTipService.Placement="Bottom">
<ToolTipService.ToolTip>
<Grid RowDefinitions="Auto,*">
<TextBlock
Grid.Row="0"
FontWeight="SemiBold"
Text="Some Title" />
<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>
答案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.
<Window
x:Class="TipTests.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:TipTests"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Button
local:ScreenTip.Description="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."
local:ScreenTip.Placement="Bottom"
local:ScreenTip.Title="LOREM IPSUM"
Content="Click" />
</Grid>
</Window>
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;
// 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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论