2012-08-01 26 views
-2
<Grid x:Name="LayoutRoot"> 
    <Button x:Name="btn_A" Content="A" HorizontalAlignment="Left" Height="36" Margin="194,160,0,0" VerticalAlignment="Top" Width="70" Click="Button_Click" /> 
    <TextBox x:Name="txt_focus" HorizontalAlignment="Left" Height="34.5" Margin="177,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="97" TextChanged="TextBox_TextChanged" /> 
</Grid> 

C#代碼下面如何使用xaml代碼將光標聚焦於wpf中的文本框?

public partial class focus : Window 
{ 
    public focus() 
    { 
     this.InitializeComponent();   
    } 

    private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
    {   
     txt_focus.Focus(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    {   

     txt_focus.Text += btn_A.Content.ToString(); 
    } 

    private void txt_focus_GotFocus(object sender, RoutedEventArgs e) 
    { 
     int count = txt_focus.Text.Length; 
     txt_focus.CaretIndex = count; 
    } 
} 

上面的代碼運作良好。但是我我需要的是,我已經實現了下面的代碼在XAML

private void TextBox_TextChanged(object sender, TextChangedEventArgs e) 
{   
    txt_focus.Focus(); 
} 
+1

什麼是「聚焦光標文本框?」 – 2012-08-01 13:24:20

+0

你是什麼意思,@Chandru A? – 2012-08-01 13:27:31

回答

0

你可以用這個代碼嘗試

<StackPanel Orientation="Vertical" 
      FocusManager.FocusedElement="{Binding ElementName=txt_focus}"> 
    <TextBox x:Name="txt_focus" 
      HorizontalAlignment="Left" 
      Height="34.5" 
      Margin="167.5,139,0,0" 
      TextWrapping="Wrap" 
      VerticalAlignment="Top" 
      Width="97" 
      TextChanged="TextBox_TextChanged" /> 
</StackPanel> 
+0

而不使用StackPanel。我該怎麼辦 ? – 2012-08-01 13:41:30

-1

您可以使用JavaScript,並使用此代碼在這裏:

if(isset($_REQUEST['sub_con_page'])) 
{ 
//correct 
} 
else 
{ ?> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $("#txt_focus").focus(); 
}); 
</script> 
<?php } ?> 
+1

-1這是關於WPF + XAML而不是PHP + HTML – GameScripting 2012-08-01 18:15:12

相關問題