-
[WPF] Custom ControlC#/WPF 2025. 8. 20. 15:08
# Custom Control With User Control
표준 정석
https://www.youtube.com/watch?v=t8zB_SYGOF0
Ex) element의 내용이 실제 사이즈보다 작을 경우 ToolTip Enable
# CustomTextBlock
using System; using System.Windows; using System.Windows.Controls; namespace MyComponents { public class CustomTextBlock : TextBlock { protected override void OnInitialized(EventArgs e) { // we want a tooltip that resizes to the contents -- a textblock with TextWrapping.Wrap will do that var toolTipTextBlock = new TextBlock(); toolTipTextBlock.TextWrapping = TextWrapping.Wrap; // bind the tooltip text to the current textblock Text binding var binding = GetBindingExpression(TextProperty); if (binding != null) { toolTipTextBlock.SetBinding(TextProperty, binding.ParentBinding); } var toolTipPanel = new StackPanel(); toolTipPanel.Children.Add(toolTipTextBlock); ToolTip = toolTipPanel; base.OnInitialized(e); } protected override void OnToolTipOpening(ToolTipEventArgs e) { if (TextTrimming != TextTrimming.None) { e.Handled = !IsTextTrimmed(); } } private bool IsTextTrimmed() { Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity)); return ActualWidth < DesiredSize.Width; } } }
Xaml
<Window ... xmlns:components="clr-namespace:MyComponents" ... > <components:CustomTextBlock Text="{Binding Details}" TextTrimming="CharacterEllipsis" />
# 자동생성
Generic.xaml
# 주석처리
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:LS.CommonUI.EngSearch.Com" xmlns:customControls="clr-namespace:LS.CommonUI.EngSearch.Com.CustomControls"> <!--<Style TargetType="{x:Type customControls:CustomTextBlock}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type customControls:CustomTextBlock}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>--> </ResourceDictionary>
# CustomBorder
#사용법 1a
: 같은 프로젝트 내에서 => 사용하려는 xaml에서 markup에 namespace선언
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:LS.CommonUI.EngSearch.Com.CustomControls"#사용법 1b
: 다른 프로젝트에서 => 사용하려는 xaml에서 markup에 namespace선언
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:LS.CommonUI.EngSearch.Com.CustomControls;assembly=LS.CommonUI.EngSearch.Com.CustomControls"
///#사용법 2
: xaml에서
/// Go ahead and use your control in the XAML file.
///
/// <MyNamespace:CustomBorder/>
///namespace LS.CommonUI.EngSearch.Com.CustomControls { /// <summary> /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file. /// /// Step 1a) Using this custom control in a XAML file that exists in the current project. /// Add this XmlNamespace attribute to the root element of the markup file where it is /// to be used: /// /// xmlns:MyNamespace="clr-namespace:LS.CommonUI.EngSearch.Com.CustomControls" /// /// /// Step 1b) Using this custom control in a XAML file that exists in a different project. /// Add this XmlNamespace attribute to the root element of the markup file where it is /// to be used: /// /// xmlns:MyNamespace="clr-namespace:LS.CommonUI.EngSearch.Com.CustomControls;assembly=LS.CommonUI.EngSearch.Com.CustomControls" /// /// You will also need to add a project reference from the project where the XAML file lives /// to this project and Rebuild to avoid compilation errors: /// /// Right click on the target project in the Solution Explorer and /// "Add Reference"->"Projects"->[Browse to and select this project] /// /// /// Step 2) /// Go ahead and use your control in the XAML file. /// /// <MyNamespace:CustomBorder/> /// /// </summary> public class CustomBorder : Border { static CustomBorder() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomBorder), new FrameworkPropertyMetadata(typeof(CustomBorder))); } protected override void OnToolTipOpening(ToolTipEventArgs e) { //if (TextTrimming != TextTrimming.None) { e.Handled = !IsTextTrimmed(); } } private bool IsTextTrimmed() { Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity)); return ActualWidth < DesiredSize.Width; } } }
<controls:CustomBorder Background="#1C0B0A15" ClipToBounds="True" ToolTip="{Binding Path=Text, ElementName=itemDesciptionTextBlock}" ToolTipService.IsEnabled="{Binding }" ToolTipService.HorizontalOffset="20" ToolTipService.VerticalOffset="-50" ToolTipService.InitialShowDelay="0" >
https://kaki104.tistory.com/851
사용자 정의 컨트롤 만들기 Custom XAML Control - part2
2022.10.06 - [WPF .NET] - 사용자 정의 컨트롤을 만들기 Custom XAML Control - part1 이전 part1에서 만들었던 컨트롤을 Custom Control를 이용해서 다시 만들어 보도록 하겠습니다. 1. CustomControl 추가 솔루션 탐색
kaki104.tistory.com
How can I determine if my TextBlock text is being trimmed?
The following textblock wraps and trims as expected. The elipsis "..." is displayed when the text is trimmed. <TextBlock MaxWidth="60" MaxHeight="60" Text="This is some long text...
stackoverflow.com
728x90'C# > WPF' 카테고리의 다른 글
[WPF] ComboBox Search History (0) 2025.08.25 [WPF] Progress, ProgressBar (1) 2025.08.25 [WPF] TargetName (1) 2025.07.09 [WPF] ComboBox Enum Description (0) 2025.06.19 [WPF] Constructor, Property Create order(생성자 프로퍼티 생성순서) (0) 2025.06.17 댓글