1

我是新来的C#XAML发展的。我创建了一个带有多个文本框的 Metro 应用程序。这些文本框通过 C# 代码中的 StackPanel 加载到 XAML 数据中,必须进行硬编码。问题是,我不知道如何在每个文本框之间添加一些空格。有人有想法吗?编码 :

 private void AddLastestCreatedField()
    {
        // Load the last created Field From DB

        DBFunction.FieldTypes latestField;

        DBFunction.Class1 myDBClass = new DBFunction.Class1();
        latestField = myDBClass.GetLastestField();

        // add new textbox and put it on the screen

        var dragTranslation = new TranslateTransform();

        //Generate the TextBox

        TextBox fieldTextBox = new TextBox();

        fieldTextBox.Name = "fieldTextBox_" + latestField.ID.ToString();
        fieldTextBox.FontSize = 15;
        fieldTextBox.Background.Opacity = 0.8;
        ToolTip toolTip = new ToolTip();
        toolTip.Content = latestField.Description;
        ToolTipService.SetToolTip(fieldTextBox, toolTip);
        fieldTextBox.IsReadOnly  = true;

        // Add Drag and Drop Handler for TextBox

        fieldTextBox.ManipulationMode = ManipulationModes.All;
        fieldTextBox.ManipulationDelta += fieldTextBox_ManipulationDelta;
        fieldTextBox.ManipulationCompleted += fieldTextBox_ManipulationCompleted;
        fieldTextBox.RenderTransform = dragTranslation;
        dragTranslationDict.Add(fieldTextBox.Name, dragTranslation);
        fieldTextBox.RenderTransform = dragTranslation;

        // Add TextBox to a List to control later
        TxtBoxList.Add(fieldTextBox);

        // Generate TextBlock for each TextBlock

        TextBlock fieldTextBlock = new TextBlock();
        // fieldTextBlock.Name = "fieldTextBlock_" + cnt.ToString();


        fieldTextBlock.TextAlignment = TextAlignment.Right;
        fieldTextBlock.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right;

        fieldTextBlock.Name = "fieldTextBlock_" + latestField.ID.ToString();
        fieldTextBlock.Text = latestField.Name;
        fieldTextBlock.FontSize = 15;
        fieldTextBlock.Height = 33;

        // Add Drag and Drop Handler for TextBlock

        var dragTranslation2 = new TranslateTransform();
        fieldTextBlock.RenderTransform = dragTranslation2;
        dragTranslationDict2.Add(fieldTextBlock.Name, dragTranslation2);

        // Add TextBlock to a list to control later

        TxtBlockList.Add(fieldTextBlock);

        TextBoxStack.Children.Add(fieldTextBox);
        TextBlockStack.Children.Add(fieldTextBlock);



    }
4

2 回答 2

1

我会跳过通常的“你试过什么?” 问题并说您可能可以通过在 上设置Margin属性来获得所需的TextBox- 该Margin属性将在控件大小周围添加“空间”作为一种填充(不要与属性混淆,这将在控件范围Padding添加空间)

于 2013-03-06T16:27:42.450 回答
1

I don't know what you are really up to, but either use the Margin-property of the textbox. It defines, how much space there will be around the control,

See MSDN for more information.

于 2013-03-06T16:28:30.690 回答