2017-09-12 14 views
0

TextBox要素を含むListBoxItemテンプレートがあります。ユーザーがTextBoxをクリックすると、ListBoxItemSelectedItemとしてListBoxにする必要があります。WPF ListBoxItem IsSelectedプロパティは瞬時にのみ選択します

<ListBox.ItemTemplate> 
     <DataTemplate> 
      <DockPanel Margin="4,0,4,0"> 
      <TextBlock Text="{Binding Path=Value1, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
         ToolTip="{Binding Path=Hint, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
         VerticalAlignment="Center" /> 
      <TextBox x:Name="TextField"        
          Margin="2,0,0,0" 
          Width="{Binding Path=ActWidth, Mode=OneWay}" 
          Visibility="{Binding Path=VisibleAct, Mode=OneWay}" 
          /> 
     </DockPanel> 
     </DataTemplate> 
</ListBox.ItemTemplate> 

私は選択を行うために、次のトリガーを持っている:私はフォーカスを変更するとき

<ListBox.Resources> 
    <Style TargetType="ListBoxItem"> 
     <Style.Triggers> 
     <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
      <Setter Property="IsSelected" Value="True" /> 
     </Trigger> 
     </Style.Triggers> 
    </Style> 
    </ListBox.Resources> 

は残念ながら、それは 一瞬のためにのみ動作し、は、選択がを消えます。 トリガーを削除すると正常に動作しますが、TextBoxを選択してもトリガーは発生しません。

選択を永久にするにはどうすればよいですか?

+0

@ mm8私は最初の投稿を修正しました。いいえ、アイテムを1つだけ選択したいが、選択を維持する。私が言及したように、私がトリガを使用せず、アイテムを手動で選択するならば、それは機能する。 – Nestor

+0

含まれているUserControl内の別のコントロールをクリックします。 – Nestor

+0

IsSelectedのデフォルトのセッターを追加して、値=バインディングにすることを試してください。 – sTrenat

答えて

1

トリガがfalseの場合、それがトリガする前に持っていたとして、あなたのListBoxItemが値を取得している、ので、あなたのサンプルが動作しません。あなたのListBoxItemは、私のことを.. "と似ています。

だから、そのIsSelected状態バインドするデフォルトのセッター追加することによって、そのSelectedValueを設定する必要があります。自分自身への結合OneWayとしてIsSelectedのデフォルトのセッターを設定

<Style TargetType="ListBoxItem"> 
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode="OneWay", RelativeSource={RelativeSource Self}}" /> 
    <Style.Triggers> 
     <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
      <Setter Property="IsSelected" Value="True" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

は仕事を行います。トリガが偽の場合、IsSelectedをそのまま設定します。

0

ListBoxSelectedItemプロパティを設定するコードを書くことができます。あなたはTextBoxためGotKeyboardFocusイベントを処理できます。

<ListBox x:Name="lb2"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <DockPanel Margin="4,0,4,0"> 
       <TextBlock Text="{Binding Path=Value1, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
         ToolTip="{Binding Path=Hint, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
         VerticalAlignment="Center" /> 
       <TextBox x:Name="TextField"        
          Margin="2,0,0,0" 
          Width="{Binding Path=ActWidth, Mode=OneWay}" 
          Visibility="{Binding Path=VisibleAct, Mode=OneWay}" 
          GotKeyboardFocus="TextField_GotKeyboardFocus" 
          /> 
      </DockPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

private void TextField_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) 
{ 
    TextBox textBox = sender as TextBox; 
    lb2.SelectedItem = textBox.DataContext; 
} 
+0

これは私が最初に思いついたものです:)マークアップ言語だけかもしれないので、非常に柔軟で強力なマークアップ言語です。私はXAMLでそれをすることができないのは本当に悲しいです。 – Nestor

+0

私は同意しがちです:)。 XAMLはマークアップ言語であり、プログラミング言語ではありません。 – mm8

+0

@Nestor私のソリューションを試しましたか?私はそれが動作すると思います – sTrenat

関連する問題