WPF自定义可双击进行编辑的文本块

WPF自定义可双击进行编辑的文本块

思路如下

  1. 模板中同时存在只显示文本的Textblock和编辑的TextBox;
  2. 控件有显示和编辑两种状态,默认为显示状态,双击进入编辑状态;
  3. 点击时记录状态,第二次点击时显示TextBox以实现进行编辑功能;

代码实现

XAML代码

  • 先创建一个自定义控件,Template定义如下
<Style TargetType="{x:Type local:TextEditable}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:TextEditable}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <TextBlock x:Name="PART_TextBlock"
                                       Background="Transparent"
                                       FontSize="{Binding FontSize, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:TextEditable}}"
                                       Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:TextEditable}}" />
                            <TextBox x:Name="PART_TextBox"
                                     Margin="-2,0,0,0"
                                     BorderThickness="0"
                                     Background="Transparent"
                                     Visibility="Collapsed"
                                     FontSize="{Binding FontSize, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:TextEditable}}"
                                     Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:TextEditable}}" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

C#代码

  • 控件的C#代码如下
 [TemplatePart(Name = "PART_TextBlock", Type = typeof(TextEditable))]
    [TemplatePart(Name = "PART_TextBox", Type = typeof(TextEditable))]
    public class TextEditable : Control
    {
        public TextEditable()
        {
            MouseLeftButtonDown += TextEditable_MouseLeftButtonDown;
            LostFocus += TextEditable_LostFocus;
        }

        private void TextEditable_LostFocus(object sender, RoutedEventArgs e)
        {
            isSelected = false;
            Background = Brushes.Transparent;
            box.Visibility = Visibility.Collapsed;
            block.Visibility = Visibility.Visible;
        }

        bool isSelected = false;
        private void TextEditable_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!isSelected)
            {
                isSelected = true;
                box.Focus();
            }
            else
            {
                box.Visibility = Visibility.Visible;
                block.Visibility = Visibility.Collapsed;
                box.Focus();
                box.SelectAll();
            }
        }

        static TextEditable()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TextEditable), new FrameworkPropertyMetadata(typeof(TextEditable)));
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(TextEditable), new PropertyMetadata(string.Empty, OnTextChanged));

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

        TextBlock block = null;
        TextBox box = null;
        public override void OnApplyTemplate()
        {
            block = GetTemplateChild("PART_TextBlock") as TextBlock;
            box = GetTemplateChild("PART_TextBox") as TextBox;
            block.LostFocus += TextEditable_LostFocus;
            box.LostFocus += TextEditable_LostFocus;
        }
    }

效果图

可编辑文本块

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值