내용 보기

작성자

관리자 (IP : 172.17.0.1)

날짜

2020-07-09 04:34

제목

[WPF] TextBox NumberLine 정보 표시 하기


TextBox LineNumber 정보 표시하기
TextBox의 사용자 DependencyProperty를 추가하여 Line정보를 저장하도록 처리하는 방식으로 표시 할 수 있다.


AttachedProperties.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
 
namespace VMSDBCheck.CommonUIs
{
    public class AttachedProperties
    {
        #region BindableLineCount AttachedProperty
        public static string GetBindableLineCount(DependencyObject obj)
        {
            return (string)obj.GetValue(BindableLineCountProperty);
        }
 
        public static void SetBindableLineCount(DependencyObject obj, string value)
        {
            obj.SetValue(BindableLineCountProperty, value);
        }
 
        // Using a DependencyProperty as the backing store for BindableLineCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BindableLineCountProperty =
            DependencyProperty.RegisterAttached(
            "BindableLineCount",
            typeof(string),
            typeof(AttachedProperties),
            new UIPropertyMetadata("1"));
 
        #endregion // BindableLineCount AttachedProperty
 
        #region HasBindableLineCount AttachedProperty
        public static bool GetHasBindableLineCount(DependencyObject obj)
        {
            return (bool)obj.GetValue(HasBindableLineCountProperty);
        }
 
        public static void SetHasBindableLineCount(DependencyObject obj, bool value)
        {
            obj.SetValue(HasBindableLineCountProperty, value);
        }
 
        // Using a DependencyProperty as the backing store for HasBindableLineCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HasBindableLineCountProperty =
            DependencyProperty.RegisterAttached(
            "HasBindableLineCount",
            typeof(bool),
            typeof(AttachedProperties),
            new UIPropertyMetadata(
                false,
                new PropertyChangedCallback(OnHasBindableLineCountChanged)));
 
        private static void OnHasBindableLineCountChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            var textBox = (TextBox)o;
            if ((e.NewValue as bool?) == true)
            {
                textBox.SizeChanged += new SizeChangedEventHandler(box_SizeChanged);
                textBox.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
                textBox.SetValue(BindableLineCountProperty, textBox.LineCount.ToString());
            }
            else
            {
                textBox.SizeChanged -= new SizeChangedEventHandler(box_SizeChanged);
                textBox.TextChanged -= new TextChangedEventHandler(TextBox_TextChanged);
            }
        }
 
        private static void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            box_SizeChanged(sender, null);
        }
 
        private static void box_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var textBox = (TextBox)sender;
            string x = string.Empty;
            for (int i = 0; i < textBox.LineCount; i++)
            {
                x += i + 1 + "\n";
            }
            textBox.SetValue(BindableLineCountProperty, x);
        }
        #endregion // HasBindableLineCount AttachedProperty
    }
}
cs

XAML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<Style x:Key="SqlQuery_TextBox" TargetType="{x:Type TextBoxBase}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBoxBase}">
                        <Border Name="Border" Background="{TemplateBinding Background}">
                            <ScrollViewer x:Name="PART_ContentHost" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
</Style>
 
 
 
<ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                    <DockPanel>
                        <TextBlock Text="{Binding ElementName=xTxt_SqlQuery, Path=(commonUIs:AttachedProperties.BindableLineCount)}" FontSize="13" MinWidth="20"/>
                        <TextBox x:Name="xTxt_SqlQuery" Text="{Binding SqlQuery, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                 commonUIs:AttachedProperties.HasBindableLineCount="True"
                                 TextWrapping="Wrap"
                                 AcceptsReturn="True"
                                 FontSize="13"
                                 Style="{StaticResource SqlQuery_TextBox}">
                            <TextBox.InputBindings>
                                <KeyBinding Key="F5" Command="{Binding SqlQueryExecuteCommand}" CommandParameter="{Binding ElementName=xTxt_SqlQuery}" />
                            </TextBox.InputBindings>
                        </TextBox>
                    </DockPanel>
</ScrollViewer>
cs

위 처럼 AttachedProperties의 사용자 DependencyProperty를 사용하여 TextBox의 LineNumber정보를 표시 할 수 있다.

출처1

출처2