catel(http://catel.codeplex.com)framework \ toolkit、C#.Net 4.0を使用してwpf、mvvmアプリケーションを使用しています。アプリケーションには、TextBlockとComboBoxを持つListBoxがあります。 ListBoxとComboBoxは、ViewModelから2つのObservableCollectionから取り込まれます。ユーザーがボタンをクリックすると、ユーザーがComboBoxからアイテムを選択したListBoxの各行を(dbに)保存する必要があります。 SelectionChangedイベントは、ListBox内のComboBoxのいずれに対しても発生しません。アイデアは、ユーザーがコンボボックス内のアイテムを選択し、そのアイテムが選択された行のたびにViewModelのリスト(ArrayListまたはIList?)に追加することです。ListBoxのComboBoxがSelectionChangedイベントを発生させない
また、ComboBoxe SelectionChangedイベントを使用しようとすると間違ったやり方はありますか?私はまた、ListBox.Itemsを反復しようとしましたが、これはハークのように思えますし、可能であればViewModelのUI要素ロジックを避けたいと思います。
XAML:
<Grid>
<StackPanel Orientation="Horizontal">
<Label Width="180">Field1</Label>
<ListBox Height="200"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding List1, Mode=OneWay}"
Name="listBox1"
SelectionMode="Single"
Width="300">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="290">
<TextBlock Width="90" Text="{Binding}"></TextBlock>
<ComboBox Width="180" ItemsSource="{Binding DataContext.List2, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" DisplayMemberPath="Field1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<catel:EventToCommand Command="{Binding SelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" DisableAssociatedObjectOnCannotExecute="False" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
のViewModelコード:コマンドに
//in the ViewModel constructor
SelectionChangedCommand = new Command<SelectionChangedEventArgs>(OnSelectionChangedCommandExecute, OnSelectionChangedCommandCanExecute);
public Command<SelectionChangedEventArgs> SelectionChangedCommand { get; private set; }
private bool OnSelectionChangedCommandCanExecute()
{
return true;
}
private void OnSelectionChangedCommandExecute(SelectionChangedEventArgs e)
{
// add or update list....
}
...中途半端そこに私を取得し、申し訳ありません...私のミス – MethodMan
。イベントが発生し、ComboBox(e.AddedItems [0] .ToString();)から選択されたアイテムが表示されますが、変更された行は表示されません。コンボボックスの選択を変更しても、リストボックスの行は選択されません。私は努力し続け、私がそれを理解するなら私は私の解決策を掲示します。ありがとうございました。 – user657527
@ user657527なぜあなたが 'EventArgument'を渡しているのか分かりますか? – Ankesh