0

我正在寻找的是用户 1. 单击画布 2. 出现一个新标签,用户可以输入文本。3. 当用户点击别处或回车时,标签完成编辑。

这是我目前所拥有的......

        private void DrawingCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
        var canvas = (Canvas)sender;
        var point = e.GetPosition(canvas);
        Label lbl = new Label();
        lbl.Content = "start typing";
        //lbl.Left = location.X;
        //lbl.Top = location.Y;
        //lbl.Focus = Boolean.TrueString;
        canvas.Children.Add(lbl);
}

任何帮助将不胜感激,谢谢!

4

2 回答 2

1

像这样试试

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Canvas x:Name="cnvs" PreviewMouseLeftButtonUp="cnvs_MouseLeftButtonUp"  Background="Transparent"></Canvas>

  private void cnvs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        var canvas = (Canvas)sender;
        var point = e.GetPosition(canvas);
        TextBox txtBox = new TextBox() {Width=80,AcceptsReturn=false };
        canvas.Children.Add(txtBox);
        Canvas.SetLeft(txtBox, point.X);
        Canvas.SetTop(txtBox, point.Y);
        txtBox.Focus();
    }

我希望这将有所帮助。

更新

var canvas = (Canvas)sender;
        var point = e.GetPosition(canvas);
        TextBox txtBox = new TextBox() {AcceptsReturn=false ,BorderThickness=new Thickness(0)};
        Binding b = new Binding("Text") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) };
        txtBox.SetBinding(TextBox.WidthProperty, b);
        canvas.Children.Add(txtBox);
        Canvas.SetLeft(txtBox, point.X);
        Canvas.SetTop(txtBox, point.Y);
        txtBox.Focus();
于 2013-04-16T06:46:46.020 回答
1

尝试将文本框宽度设置为 Double.NaN。这将为您提供“自动”宽度。要摆脱边框,请将文本框的边框厚度设置为 0。见下文:

 private void cnvs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        var canvas = (Canvas)sender;
        var point = e.GetPosition(canvas);
        var txtBox = new TextBox() { Width=Double.NaN, AcceptsReturn=false }; // auto width
        txtBox.BorderThickness = new Thickness(0); // no border
        canvas.Children.Add(txtBox);
        Canvas.SetLeft(txtBox, point.X);
        Canvas.SetTop(txtBox, point.Y);
        txtBox.Focus();
    }
于 2013-04-16T07:10:01.010 回答