2012-06-28 14 views
20

क्या यह संभव है, ऑटोफोकस को मेरी xaml फ़ाइल में टेक्स्टबॉक्स पर सेट करना संभव है?केवल xaml में ऑटोफोकस कैसे सेट करें?

<Window x:Class="WpfApplication1.Views.Test1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      Height="100" 
      Width="210" 
      WindowStartupLocation="CenterOwner" 
      ShowInTaskbar="False" 
      Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" 
      ResizeMode="CanResizeWithGrip"> 
    <TextBox HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" AcceptsReturn="True" Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
</Window> 

उत्तर

17
<TextBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" /> 
+0

धन्यवाद, यही वह है जिसे मैं – David

+3

ढूंढ रहा था, मुझे नहीं पता कि यह मेरे लिए क्यों काम करता है। हालांकि, इस संलग्न संपत्ति को हेडर में एलीमेंटनाम के साथ बाइंडिंग के साथ रखने के लिए मेरे लिए काम करता है। बाध्यकारी के बिना, यह एक अपवाद फेंकता है। – newman

+1

कुछ कारणों से काम नहीं करता है जब 'टेक्स्टबॉक्स' विंडो में एकमात्र नियंत्रण नहीं है। –

13

हाँ आप FocusManager.FocusedElement जुड़ी संपत्ति का उपयोग कर सकते हैं। मैं आमतौर पर सी #

textBox1.Focus(); 

के साथ इस को हल

FocusManager.FocusedElement="{Binding ElementName=textBox1}" 
+2

वहाँ किसी भी तरह से, यह एक नाम के बिना बनाने के लिए किया जाता है? – David

5

इस

<Window x:Class="WpfApplication18.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="500" Width="525" FocusManager.FocusedElement="textcontrol"> 
    <Grid> 
     <TextBox Name="textcontrol" /> 
    </Grid> 
</Window> 
+3

कोई नाम नहीं है, इसे बिना नाम के बनाने के लिए? – David

+1

FocusManager.FocusedElement = "{बाध्यकारी ElementName = textcontrol}" – anevil

-6

तरह somethind कोशिश मुझे लगता है कि यह सबसे अच्छा तरीका है कि यह कैसे करना है।

+0

जो समस्या को हल करने का एक गंदा तरीका है ... – David

+1

यह समाधान सरल, पठनीय और स्पष्ट है। –

+3

@ कोलोनेल पैनिक हाँ, लेकिन यह नहीं पूछा गया था कि यहां क्या पूछा गया था। –

4

मैं बंधन लगता है, एक overkill है संदर्भ अधिक हल्के है:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     FocusManager.FocusedElement="{x:Reference textBox1}"> 
    <StackPanel> 
     <TextBox x:Name="textBox1" /> 
    </StackPanel> 
</Window> 
+0

क्या इस और बाध्यकारी के बीच वास्तव में कोई प्रदर्शन अंतर है? – CodeHxr

संबंधित मुद्दे