私はこれを今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テンプレートとのインタラクティブ機能も使用しようとしましたが、コマンドは実行されませんでした。
アイデア?