将xaml文本框代码转换为c#文本框代码?

时间:2013-08-14 05:07:20

标签: c# xaml

我开发了一个带有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 txt1 = new TextBox()创建了TextBox。但我不知道如何使用上面的XAML TextBox属性创建文本框。

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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

答案 2 :(得分:0)

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

这样的东西
TextBox txt1 = new TextBox();
tx1.Name = "tx1";

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

相关问题