【问题标题】:How to set focus in a textbox when user click an expander?用户单击扩展器时如何在文本框中设置焦点?
【发布时间】:2011-10-15 04:44:09
【问题描述】:

我有一个扩展器...和一个文本框。默认情况下,文本框是隐藏的。当用户单击扩展器时,我正在显示文本框。这工作正常。

我需要的是,当用户单击扩展器时...我需要在文本框中设置焦点。

请帮我这样做...我尝试使用以下代码...但似乎“IsFocused”是只读属性。

任何帮助将不胜感激!

<StackPanel>
  <DockPanel>
    <TextBlock DockPanel.Dock="Left"  Text="ID"/>
      <Expander x:Name="ID" DockPanel.Dock="Right" IsExpanded="False" ExpandDirection="Down">                                                    
      </Expander>
  </DockPanel>
  <TextBox Text="{Binding Path=SearchCCCId.Value,UpdateSourceTrigger=PropertyChanged}" 
     Visibility="{Binding ElementName=ID,Path=IsExpanded,Converter={x:Static local:Converters.BoolToVisibility}}"  Width="70" >
   <TextBox.Style>
     <Style>
        <Style.Triggers>
           <DataTrigger Binding="{Binding ElementName=ID, Path=IsEpanded}" Value="True" >
              <Setter Property="IsFocused" Value="True" />
           </DataTrigger>
         </Style.Triggers>
     </Style>
    </TextBox.Style>
   </TextBox>
</StackPanel>

【问题讨论】:

    标签: wpf textbox setfocus


    【解决方案1】:

    在你的帮助下,我能够找到答案......

    <TextBox.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=ID, Path=IsExpanded}" Value="True">
                    <Setter Property="FocusManager.FocusedElement" 
                            Value="{Binding ElementName=PropertyIDSearch}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        您可以处理 Expander 的 Expanded 事件并在处理程序方法中设置焦点。

        private void OnExpanderExpanded(object sender, RoutedEventArgs args)
        {
            txtTest.Focus();
        }
        

        【讨论】:

          猜你喜欢
          • 2017-01-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-10
          • 1970-01-01
          • 2013-08-21
          • 1970-01-01
          相关资源
          最近更新 更多