1

我已经开发了一个带有 XAML 代码的文本框,但现在我需要将其转换为 C# 代码;但我不熟悉它。所以,特此分享我的代码,并请求大家将其转换为 C# 代码;

我的 XAML 代码;

<TextBox Name="txt1" Height="72" Width="130" Margin="193,3,0,0" InputScope="Number" MaxLength="3"/>
<TextBox Name="txt2" Height="72" Width="130" Margin="193,68,0,0" InputScope="Number" MaxLength="3"/>

我通过 c# 代码创建了 TextBox TextBox txt1 = new TextBox()。但我不知道如何使用上述 XAML 文本框属性创建文本框。

4

3 回答 3

1

您可以简单地从工具箱中拖放。但是,您可以通过代码执行此操作。

System.Windows.Forms.TextBox txt1 = = new System.Windows.Forms.TextBox();
txt1.Location = new System.Drawing.Point(78, 183);
this.txt1.MaxLength = 3;
this.txt1.Multiline = true;
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(130, 72);
this.txt1.TabIndex = 2;
this.Controls.Add(this.txt1);

现在 InputScope = Number

这里有几种方法。您可以使用MaskedTextBox,也可以通过验证用户的输入来限制用户。第一个是首选。

System.Windows.Forms.MaskedTextBox txt1= new System.Windows.Forms.MaskedTextBox();
this.txt1.Location = new System.Drawing.Point(149, 141);
this.txt1.Mask = "000";
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(100, 20);
this.txt1.TabIndex = 3;
this.txt1.ValidatingType = typeof(int);
this.Controls.Add(this.txt1);
于 2013-08-14T05:16:14.327 回答
1

你可以这样做..

 TextBlock txt1 = new TextBlock();
        txt1.Height = 72;
        txt1.Width = 72;
        txt1.Margin = new Thickness(193, 3, 0, 0);
        InputScope inputScope = new InputScope();
        InputScopeName inputScopeName = new InputScopeName();
        inputScopeName.NameValue = InputScopeNameValue.Number;

        inputScope.Names.Add(inputScopeName);
        txt1.InputScope = inputScope;
        SomeGrid.Children.Add(txt1); // somegrid is a parent control in which you wanted to add your textblock
于 2013-08-14T05:21:52.563 回答
0

您可以按照您提到的相同方式创建 TextBox,然后您可以设置属性。就像是

TextBox txt1 = new TextBox();
tx1.Name = "tx1";

您应该能够像这样设置大多数属性。对于一些复杂的属性,您可能必须创建某种类型的实例然后设置它。

于 2013-08-14T05:16:33.787 回答