![](/img/trans.png)
[英]C# / XAML: How to declare a textbox in XAML code that is used in a class
[英]conversion of xaml textbox code to c# textbox code?
我已經用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 Properties創建文本框。
您可以簡單地從工具箱中拖放。 但是,您可以通過代碼這樣做。
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);
你可以這樣
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
您可以按照前面提到的方法創建TextBox,然后可以設置屬性。 就像是
TextBox txt1 = new TextBox();
tx1.Name = "tx1";
您應該能夠像這樣設置大多數屬性。 對於一些復雜的屬性,您可能必須創建某種類型的實例,然后進行設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.