2009-09-10 43 views

回答

10

如果你想要一個像組合框或日期時間選擇器的東西,你應該創建一個新的控件,在這個新的控件裏面放置一個文本框和一個按鈕並排放置在一個看起來像文本框框架的框架中 - 然後restyle的文本框,所以它沒有框架。

把一個按鈕,豐富的編輯中,如果你想要把一個按鈕內部「文件」,但不是一個很好的替代品的組合框是偉大的。

見ComboBox控件模板MSDN

+0

其實我選擇了這條路到底。我使用了Border元素來模擬文本框外邊框,並使用Grid將真正的無邊框文本框和按鈕放入其中。 – 2009-09-10 15:57:11

+0

+1用於鏈接到組合框模板。 – 2010-09-01 14:08:05

0

您可以使用RichTextBox而不是文本框,並且它支持可以在其中放置按鈕的flowdocument。

0

你也可以使用一個標籤,並改變它的模板,包括在它的按鈕。要詳細瞭解Label和TextBlock之間的差異,請參閱this post

5

我創建一個文本框控件,並將此 看來工作,但不理想的情況下因爲這一切重新創建另一文本框。

<TextBox.Template> 
<ControlTemplate> 
     <Grid> 
      <Grid.ColumnDefinitions></Grid.ColumnDefinitions> 
      <TextBox Grid.Column="0"></TextBox> 
      <Button HorizontalAlignment="Right" Width="25" Grid.Column="1"> 
      </Button> 
     </Grid>   
    </ControlTemplate> 
</TextBox.Template> 
4

您可能會感興趣此鏈接help:http://msdn.microsoft.com/en-us/library/ms752068(VS.85).aspx

「TextBox的ControlTemplate必須只包含一個標記爲內容主機元素的元素;該元素將用於呈現TextBox的內容;要將元素標記爲內容主機,請將其指定爲特殊命名PART_ContentHost,內容主機元件必須是的ScrollViewer或AdornerDecorator,內容主機元件可以不承載任何子元素「。

0

這樣做的正確方法是使用一個控制模板上的文本框中。像下面的東西。我在一個從文本框繼承的類中使用了它,並將其稱爲ButtonBox。然後,我從這個如DateBox,DateTimeBox,SqlServerConnectBox等

xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 

<TextBox.Template> 
    <ControlTemplate TargetType="{x:Type TextBoxBase}"> 
     <mwt:ListBoxChrome 
      Background="{TemplateBinding Panel.Background}" 
      BorderBrush="{TemplateBinding Border.BorderBrush}" 
      BorderThickness="{TemplateBinding Border.BorderThickness}" 
      RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" 
      RenderFocused="{TemplateBinding UIElement.IsKeyboardFocusWithin}" 
      Name="Bd" 
      SnapsToDevicePixels="True"> 

      <DockPanel> 
       <Button DockPanel.Dock="Right" Name="myButton" Padding="3,0" Click="myButton_Click">...</Button> 
       <ScrollViewer Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"></ScrollViewer> 
      </DockPanel> 
     </mwt:ListBoxChrome> 
     <ControlTemplate.Triggers> 
      <Trigger Property="UIElement.IsEnabled"> 
       <Setter Property="Panel.Background" TargetName="Bd"> 
        <Setter.Value> 
         <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" /> 
        </Setter.Value> 
       </Setter> 
       <Setter Property="TextElement.Foreground"> 
        <Setter.Value> 
         <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> 
        </Setter.Value> 
       </Setter> 
       <Trigger.Value> 
        <s:Boolean>False</s:Boolean> 
       </Trigger.Value> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
</TextBox.Template> 

編輯繼承別人:我改變我所用的方法,以便從控制而不是文本框繼承。這很好,因爲控件只包含邊框,文本框和按鈕。上述解決方案讓我感到焦點問題。這是新的模板,我稱我的控件爲ButtonBox

<Style TargetType="{x:Type local:ButtonBox}"> 
    <Setter Property="Border.BorderThickness" Value="1"></Setter> 
    <Setter Property="Border.BorderBrush"> 
     <Setter.Value> 
      <LinearGradientBrush StartPoint="0,0" EndPoint="0,20" MappingMode="Absolute"> 
       <LinearGradientBrush.GradientStops> 
        <GradientStop Color="#FFABADB3" Offset="0.05" /> 
        <GradientStop Color="#FFE2E3EA" Offset="0.07" /> 
        <GradientStop Color="#FFE3E9EF" Offset="1" /> 
       </LinearGradientBrush.GradientStops> 
      </LinearGradientBrush> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Control.Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:ButtonBox}"> 
       <mwt:ListBoxChrome 
        Background="{TemplateBinding Panel.Background}" 
        BorderThickness="{TemplateBinding Border.BorderThickness}" 
        BorderBrush="{TemplateBinding Border.BorderBrush}" 
        RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" 
        RenderFocused="{TemplateBinding UIElement.IsKeyboardFocusWithin}" 
        Name="Bd" 
        SnapsToDevicePixels="True"> 
        <DockPanel> 
         <Button 
          DockPanel.Dock="Right" 
          Name="PART_Button" 
          Height="0" 
          Style="{x:Null}" 
          Margin="0" 
          Padding="3,0" 
          Content="{TemplateBinding local:ButtonBox.ButtonContent}" 
          IsTabStop="False">         
         </Button> 
         <TextBox 
          BorderBrush="{x:Null}" 
          BorderThickness="0" 
          Margin="0" 
          Name="PART_ContentHost" 
          IsReadOnly="{TemplateBinding TextBox.IsReadOnly}" 
          Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Path=Text}">         
         </TextBox> 
         <!-- ScrollViewer Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" Margin="1"></ScrollViewer --> 
        </DockPanel> 
       </mwt:ListBoxChrome> 
       <ControlTemplate.Triggers> 
        <Trigger Property="UIElement.IsEnabled"> 
         <Setter Property="Panel.Background" TargetName="Bd"> 
          <Setter.Value> 
           <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" /> 
          </Setter.Value> 
         </Setter> 
         <Setter Property="TextElement.Foreground"> 
          <Setter.Value> 
           <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> 
          </Setter.Value> 
         </Setter> 
         <Trigger.Value> 
          <s:Boolean>False</s:Boolean> 
         </Trigger.Value> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="IsTabStop" Value="False"></Setter> 
</Style> 
-1

您可以使用網格來完成此任務。下面是我如何創建它出現在文本框的右下角的按鈕:

<Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 

     <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="0" /> 

     <Button Content="Copy" Width="40" Height="40" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="10" Grid.Row="0" /> 

</Grid> 
0

只需使用Grid.Column同樣喜歡下面的代碼

<TextBox x:Name="txtUrl" Grid.Column="1" Margin="2,2,0,2" VerticalAlignment="Center" Padding="2" PreviewKeyDown="txtUrl_PreviewKeyDown" GotFocus="txtUrl_GotFocus" PreviewMouseDown="txtUrl_PreviewMouseDown"> 

</TextBox> 
<eo:BareButton x:Name="btnAddFavorite" Grid.Column=" 1" HorizontalAlignment="Right" Style="{StaticResource WindowButtonStyle }" Margin="2" > 
    <eo:BareButton.Template> 
     <ControlTemplate TargetType="{x:Type eo:BareButton}"> 
      <Border x:Name="PART_Border" Width="22" Height="22" Background="Transparent" VerticalAlignment="Center" Margin="2,0,0,0" CornerRadius="2"> 
       <Path 
      HorizontalAlignment="Center" 
      VerticalAlignment="Center" 
                  Fill="Yellow" 

      Data="M 2,9 L 8,8 10,2 13,8 19,9 15,13 16,19 10,15 5,19 6,13 2,9" 
     SnapsToDevicePixels="false" 
      Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type eo:BareButton}, Mode=FindAncestor}}" 
      StrokeThickness="1" /> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter TargetName="PART_Border" Property="BorderBrush" Value="#666"/> 
        <Setter TargetName="PART_Border" Property="BorderThickness" Value="1"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </eo:BareButton.Template> 

</eo:BareButton> 
相關問題