2016-08-04 14 views
1

UWPアプリケーション(Windows 10)では、ListViewにレコードのリストを表示しています。ListViewアイテムのTextBoxでフォーカスを設定する方法は?

アイテムをクリックすると、StackPanelが表示されます(INotifyPropertyChangedを使用)。 StackPanelには、バインドによってデータが入力されたTextBoxがあります。

StackPanelが表示されるたびにTextBoxが自動的にフォーカスを受け取りますが、使用するプロパティやイベント、およびtextBox.Focus()をトリガーする方法を見つけることができません。

ありがとうございます。

のDataTemplate:

<DataTemplate x:Key="templateList"> 
     <StackPanel> 
... 
      <StackPanel Visibility="{Binding IsSelected}"> 
       <TextBox x:Name="textBox" 
         Text="{Binding Title, Mode=TwoWay}"/> 
... 
      </StackPanel> 
     </StackPanel> 
    </DataTemplate> 
... 

リストビュー:

<ListView x:Name="listView" 
      ItemsSource="{Binding mylist}" 
      ItemTemplate="{StaticResource templateList}"/> 

答えて

2

私はこのような場合のために使用Behaviorsを提案することができます。私が気づいたようにIsSelectedプロパティにはVisibilityタイプを使用します。このため

xmlns:i="using:Microsoft.Xaml.Interactivity" 
xmlns:core="using:Microsoft.Xaml.Interactions.Core" 

... 

<StackPanel Visibility="{Binding IsSelected}" 
      Grid.Row="1"> 
    <TextBox x:Name="textBox" 
       Text="{Binding Title, Mode=TwoWay}"> 
     <i:Interaction.Behaviors> 
      <core:DataTriggerBehavior Binding="{Binding IsSelected}" 
             ComparisonCondition="Equal" 
             Value="Visible"> 
       <local:SetupFocusAction TargetObject="{Binding ElementName=textBox}"/> 
      </core:DataTriggerBehavior> 
     </i:Interaction.Behaviors> 
    </TextBox> 
</StackPanel> 

enter image description here

+0

感謝を:それは私たちがDataTriggerBehaviorを使用してIAction

public class SetupFocusAction : DependencyObject, IAction { public Control TargetObject { get { return (Control)GetValue(TargetObjectProperty); } set { SetValue(TargetObjectProperty, value); } } public static readonly DependencyProperty TargetObjectProperty = DependencyProperty.Register("TargetObject", typeof(Control), typeof(SetupFocusAction), new PropertyMetadata(0)); public object Execute(object sender, object parameter) { return TargetObject?.Focus(FocusState.Programmatic); } } 

その後、我々はXAMLでこのアクションを使用することができます実装する私たちのSetupFocusActionを作成できることを意味します! CS0246 \t型または名前空間の名前 'IAction'が見つかりませんでした(usingディレクティブまたはアセンブリ参照がありませんか?)。次のエラーが発生しました。どのディレクティブを使用する必要がありますか?ビヘイビアを使用するには特定の参照を追加する必要がありますか? – Daniel

+0

[Nuget](https://www.nuget.org/packages/Microsoft.Xaml.Behaviors.Uwp.Managed/)からライブラリを追加できます。 –

+0

私は自分のプロジェクト用にTemplate10ライブラリをインストールしましたが、すべて正常に動作します!助けてくれてありがとう:-) – Daniel

関連する問題