2017-10-08 7 views
0

私はこれを今2日間把握しようとしています。 ObservableCollectionにバインドされたListBoxがあります。 ListBoxはテンプレートを使用して、コレクション内の各アイテムのButtonを表示しています。コマンドを起動し、ボタンをクリックするときにselectedItemをパラメータとして渡したいと思います。ボタンだけを含むitemtemplateを持つListBoxでSelectedItemを取得する

問題は、listBoxのSelectedItemがnullのボタンをクリックしたときに、リストボックスがその選択を変更するフォーカスを取得しないことです。私が見つけたので、ボタンのクリックイベントはListBox SelectionChangedを中断しています。 ここはlistBoxのXAMLです。私のビューモデルはPrism 6.3 DelegateCommandを使ってイベントをパブリッシュします。

<ListBox Name="PatientsListBox" 
     ItemsSource="{Binding Patients}" 
     SelectedItem="{Binding SelectedPatient}" 
     HorizontalContentAlignment="Stretch" 
     > 

     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Button Content="{Binding Path=Name}" 
          Background="{Binding Path=Sex, Converter={StaticResource ColorConverter}}" 
          Margin="0" Padding="0" 
          Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=ActualWidth, Mode=OneWay}" 
          Command="{Binding ElementName=LayoutRoot, Path=DataContext.ShowPatientCommand}" 
          CommandParameter="{Binding ElementName=PatientsListBox, Path=SelectedItem}"> 
        </Button> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 

XAMLを使用するだけでコードなしでこれを行うことはできますか?

ItemsControlを使用しようとしましたが、SelectedItemプロパティがありません。 私はtextBlockテンプレートとのインタラクティブ機能も使用しようとしましたが、コマンドは実行されませんでした。

アイデア?

答えて

0

最終的にMSDNフォーラムHere is the full threadで回答が見つかりました。私は、これはDataTemplateをのバウンドアイテムを使用するWPFコマンドマネージャに指示し、次の

CommandParameter="{Binding}" 

に結合する私のCommandParameterを変更しました。 MiguelFernándezCorral

これはそれです。私はこれが将来誰かを助けることを願っています。 歓迎

関連する問題