2009-09-10 13 views
11

मैं TextBox को Button के अंदर DatePicker के साथ बनाना चाहता हूं, लेकिन बिल्कुल नहीं। या के अंदर ComboBox हो सकता है, ताकि आप TextBox के मोड को स्विच कर सकें।डब्ल्यूपीएफ में एक बटन के साथ टेक्स्टबॉक्स कैसे बनाएं?

क्या आप मेरी मदद कर सकते हैं?

उत्तर

10

यदि आप एक कम्बोबॉक्स या डेट टाइम पिकर की तरह कुछ चाहते हैं तो आपको एक नया नियंत्रण बनाना चाहिए, इस नए नियंत्रण के अंदर टेक्स्ट बॉक्स के फ्रेम की तरह दिखने वाले फ्रेम के अंदर एक टेक्स्ट बॉक्स और एक बटन साइड रखें। टेक्स्टबॉक्स को पुन: स्थापित करें ताकि इसमें कोई फ्रेम न हो।

एक समृद्ध संपादन के अंदर एक बटन डालना बहुत अच्छा है यदि आप "दस्तावेज़" के अंदर एक बटन रखना चाहते हैं लेकिन एक combobox के लिए एक अच्छा विकल्प नहीं है।

देखें ComboBox नियंत्रण टेम्पलेट MSDN

+0

नीचे की तरह मैं वास्तव में अंत में इस तरह से चुना Grid.Column का उपयोग करें: निम्न कैसे मैं एक बटन जो किसी पाठ बॉक्स के दाईं नीचे दिखाई देने वाले बनाई गई है। मैंने टेक्स्ट बॉक्स बाहरी सीमा का अनुकरण करने के लिए सीमा तत्व का उपयोग किया और वास्तविक सीमा-रहित टेक्स्टबॉक्स और ग्रिड का उपयोग करके एक बटन डाला। Combobox टेम्पलेट के लिंक के लिए –

+0

+1। –

0

आप टेक्स्टबॉक्स के बजाय RichTextBox का उपयोग कर सकते हैं और यह फ़्लॉड दस्तावेज़ का समर्थन करता है जिसमें आप बटन डाल सकते हैं।

0

तुम भी एक लेबल का उपयोग करें और उस में एक बटन शामिल करने के लिए अपने टेम्पलेट बदल सकते हैं। लेबल और टेक्स्टब्लॉक के बीच मतभेदों का अच्छा अवलोकन करने के लिए 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

आपको यह लिंक मिल सकता है: http://msdn.microsoft.com/en-us/library/ms752068(VS.85).aspx

"टेक्स्टबॉक्स के लिए नियंत्रण टेम्पलेट में एक तत्व होना चाहिए जिसमें सामग्री होस्ट तत्व के रूप में टैग किया गया हो; इस तत्व का उपयोग टेक्स्टबॉक्स की सामग्री को प्रस्तुत करने के लिए किया जाएगा। सामग्री होस्ट के रूप में तत्व को टैग करने के लिए, इसे विशेष असाइन करें नाम PART_ContentHost। सामग्री होस्ट तत्व या तो स्क्रॉल व्यूअर या एडॉर्नर डिकोरेटर होना चाहिए। सामग्री होस्ट तत्व किसी भी बच्चे तत्वों को होस्ट नहीं कर सकता है। "

0

ऐसा करने का सही तरीका टेक्स्टबॉक्स पर नियंत्रण टेम्पलेट का उपयोग करना है। नीचे कुछ पसंद है। मैंने इसे कक्षा के अंदर उपयोग किया जो टेक्स्टबॉक्स से विरासत में मिला और इसे बटनबॉक्स कहा जाता है। मैं तो इस तरह के 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> 

संपादित करने से दूसरों के वारिस: मैं विधि मैं उपयोग कर रहा था बदलें कि वह नियंत्रण और नहीं पाठ बॉक्स से विरासत है। यह अच्छी तरह से काम करता है क्योंकि नियंत्रण में केवल एक सीमा, एक टेक्स्टबॉक्स और एक बटन होता है। मुझे उपर्युक्त समाधान के साथ फोकस मुद्दे मिल रहे थे। यह नया टेम्पलेट है, मैंने अपना नियंत्रण एक बटन बॉक्स

<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

बस एक ही कोड

<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> 
संबंधित मुद्दे