2013-04-23 86 views
0

这里是我当前的代码,我不愉快的结果。出于某种原因,TextBox控件不会填充整个GridCell。这些盒子很小,并试图设置文本框的宽度/高度,以使它们变大,但它根本不起作用。我也尝试设置对齐来拉伸,但这也没有帮助。任何提示/建议如何我可以得到这个工作,将不胜感激。如何使一个TextBox fillout整个网格单元格中的XAML

//create the grid 
     Grid backGrid = new Grid(); 
     backGrid.Width = 380; 
     backGrid.Height = 200; 
     backGrid.HorizontalAlignment = HorizontalAlignment.Center; 
     backGrid.VerticalAlignment = VerticalAlignment.Center; 
     backGrid.ShowGridLines = false; 

     //define columns 
     for (int c = 0; c < 10; c++) 
     { 
      ColumnDefinition colDef = new ColumnDefinition(); 
      backGrid.ColumnDefinitions.Add(colDef); 
     } 

     //define rows 
     for (int r = 0; r < 6; r++) 
     { 
      RowDefinition rowDef = new RowDefinition(); 
      backGrid.RowDefinitions.Add(rowDef); 
     } 

     //create textboxes 
     for (int c = 0; c < 10; c++) 
     { 
      for (int r = 0; r < 6; r++) 
      { 

       TextBox txtBox = new TextBox(); 
       txtBox.FontSize = 16; 
       txtBox.Margin = new Thickness(1, 1, 1, 1); 
       txtBox.BorderThickness = new Thickness(0); 

       txtBox.Background = new SolidColorBrush(Colors.Red); 

       Grid.SetRow(txtBox, r); 
       Grid.SetColumn(txtBox, c); 

       backGrid.Children.Add(txtBox); 
      } 
     } 

     //add the grid into the mainpage 
     ContentPanel.Children.Add(backGrid); 

回答

1

它是默认的TextBox Margin。描述于TextBox Template

以下XAML显示TextBoxcontrol的默认样式和模板。

Style TargetType="TextBox"> 
<Setter Property="BorderThickness" Value="1"/> 
<Setter Property="Background" Value="#FFFFFFFF"/> 
<Setter Property="Foreground" Value="#FF000000"/> 
<Setter Property="Padding" Value="2"/> 
<Setter Property="BorderBrush"> 
    <Setter.Value> 
    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
     <GradientStop Color="#FFA3AEB9" Offset="0"/> 
     <GradientStop Color="#FF8399A9" Offset="0.375"/> 
     <GradientStop Color="#FF718597" Offset="0.375"/> 
     <GradientStop Color="#FF617584" Offset="1"/> 
    </LinearGradientBrush> 
    </Setter.Value> 
</Setter> 
<Setter Property="Template"> 
    <Setter.Value> 
    <ControlTemplate TargetType="TextBox"> 
     <Grid x:Name="RootElement"> 
     <vsm:VisualStateManager.VisualStateGroups> 
      <vsm:VisualStateGroup x:Name="CommonStates"> 
      <vsm:VisualState x:Name="Normal"/> 
      <vsm:VisualState x:Name="MouseOver"> 
       <Storyboard> 
       <ColorAnimation Storyboard.TargetName="MouseOverBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" To="#FF99C1E2" Duration="0"/> 
       </Storyboard> 
      </vsm:VisualState> 
      <vsm:VisualState x:Name="Disabled"> 
       <Storyboard> 
       <DoubleAnimation Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 
       </Storyboard> 
      </vsm:VisualState> 
      <vsm:VisualState x:Name="ReadOnly"> 
       <Storyboard> 
       <DoubleAnimation Storyboard.TargetName="ReadOnlyVisualElement" Storyboard.TargetProperty="Opacity" To="1" Duration="0" /> 
       </Storyboard> 
      </vsm:VisualState> 
      </vsm:VisualStateGroup> 
      <vsm:VisualStateGroup x:Name="FocusStates"> 
      <vsm:VisualState x:Name="Focused"> 
       <Storyboard> 
       <DoubleAnimation Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 
       </Storyboard> 
      </vsm:VisualState> 
      <vsm:VisualState x:Name="Unfocused"> 
       <Storyboard> 
       <DoubleAnimation Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/> 
       </Storyboard> 
      </vsm:VisualState> 
      </vsm:VisualStateGroup> 
      <vsm:VisualStateGroup x:Name="ValidationStates"> 
      <vsm:VisualState x:Name="Valid"/> 
      <vsm:VisualState x:Name="InvalidUnfocused"> 
       <Storyboard> 
       <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> 
        <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
        </DiscreteObjectKeyFrame> 
       </ObjectAnimationUsingKeyFrames> 
       </Storyboard> 
      </vsm:VisualState> 
      <vsm:VisualState x:Name="InvalidFocused"> 
       <Storyboard> 
       <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> 
        <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <Visibility>Visible</Visibility> 
        </DiscreteObjectKeyFrame.Value> 
        </DiscreteObjectKeyFrame> 
       </ObjectAnimationUsingKeyFrames> 
       <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsOpen"> 
        <DiscreteObjectKeyFrame KeyTime="0"> 
        <DiscreteObjectKeyFrame.Value> 
         <sys:Boolean>True</sys:Boolean> 
        </DiscreteObjectKeyFrame.Value> 
        </DiscreteObjectKeyFrame> 
       </ObjectAnimationUsingKeyFrames> 
       </Storyboard> 
      </vsm:VisualState> 
      </vsm:VisualStateGroup> 
     </vsm:VisualStateManager.VisualStateGroups> 
     <Border x:Name="Border" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1" Opacity="1" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"> 
      <Grid> 
      <Border x:Name="ReadOnlyVisualElement" Opacity="0" Background="#5EC9C9C9"/> 
      <Border x:Name="MouseOverBorder" BorderThickness="1" BorderBrush="Transparent"> 
       <ScrollViewer x:Name="ContentElement" Padding="{TemplateBinding Padding}" BorderThickness="0" IsTabStop="False"/> 
      </Border> 
      </Grid> 
     </Border> 
     <Border x:Name="DisabledVisualElement" Background="#A5F7F7F7" BorderBrush="#A5F7F7F7" BorderThickness="{TemplateBinding BorderThickness}" Opacity="0" IsHitTestVisible="False"/> 
     <Border x:Name="FocusVisualElement" BorderBrush="#FF6DBDD1" BorderThickness="{TemplateBinding BorderThickness}" Margin="1" Opacity="0" IsHitTestVisible="False"/> 
     <Border x:Name="ValidationErrorElement" BorderThickness="1" CornerRadius="1" BorderBrush="#FFDB000C" Visibility="Collapsed"> 
      <ToolTipService.ToolTip> 
      <ToolTip x:Name="validationTooltip" Template="{StaticResource ValidationToolTipTemplate}" Placement="Right" 
        PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" 
        DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"> 
       <ToolTip.Triggers> 
       <EventTrigger RoutedEvent="Canvas.Loaded"> 
        <EventTrigger.Actions> 
        <BeginStoryboard> 
         <Storyboard> 
         <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsHitTestVisible"> 
          <DiscreteObjectKeyFrame KeyTime="0"> 
          <DiscreteObjectKeyFrame.Value> 
           <sys:Boolean>true</sys:Boolean> 
          </DiscreteObjectKeyFrame.Value> 
          </DiscreteObjectKeyFrame> 
         </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </BeginStoryboard> 
        </EventTrigger.Actions> 
       </EventTrigger> 
       </ToolTip.Triggers> 
      </ToolTip> 
      </ToolTipService.ToolTip> 
      <Grid Width="12" Height="12" HorizontalAlignment="Right" Margin="1,-4,-4,0" VerticalAlignment="Top" Background="Transparent"> 
      <Path Margin="1,3,0,0" Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="#FFDC000C"/> 
      <Path Margin="1,3,0,0" Data="M 0,0 L2,0 L 8,6 L8,8" Fill="#ffffff"/> 
      </Grid> 
     </Border> 
     </Grid> 
    </ControlTemplate> 
    </Setter.Value> 
</Setter> 

你需要为TextBox创造新的风格(在Expression Blend中右键点击TextBox - > EditTemplate-> ESIT副本),并修改它不过你想要的。
对于删除默认的保证金使用这种风格

<Style x:Key="NewTextBoxStyleWithoutMargins" TargetType="TextBox"> 
     <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/> 
     <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/> 
     <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/> 
     <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/> 
     <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
     <Setter Property="Padding" Value="2"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="TextBox"> 
        <Grid Background="Transparent"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="ReadOnly"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0"> 
               <DiscreteObjectKeyFrame.Value> 
                <Visibility>Collapsed</Visibility> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ReadonlyBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0"> 
               <DiscreteObjectKeyFrame.Value> 
                <Visibility>Visible</Visibility> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ReadonlyBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ReadonlyBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="MainBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBorderBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Unfocused"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border x:Name="MainBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="0"/> 
         <Border x:Name="ReadonlyBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="0" Visibility="Collapsed"/> 
         <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="0"> 
          <ContentControl x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="0" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch"/> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

我只是更换Margin="{StaticResource PhoneTextBoxInnerMargin}"Margin="0"

这种风格正好被设置为您TextBox
希望它的帮助。

相关问题