2012-01-05 55 views
1

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.... 
} 

答えて

3

あなたはバインディング相対ソースを持っているバインディングを使用している結合...

は、これらを行うことを検討拘束力の変化

1)Ancestortype

2としてリストボックスを使用して)使用のパスを結合しながら= DataContext.SelectionChangedCommandは、それ以外の場合は、DataContextのようリストボックスがかかりますが。私の目は、これはWPFアプリケーションだったことを確認することができなかったいくつかの理由で私の悪いBathineni Dangit

<catel:EventToCommand Command="{Binding Path=DataContext.SelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}" DisableAssociatedObjectOnCannotExecute="False" PassEventArgsToCommand="True" /> 
+0

...中途半端そこに私を取得し、申し訳ありません...私のミス – MethodMan

+0

。イベントが発生し、ComboBox(e.AddedItems [0] .ToString();)から選択されたアイテムが表示されますが、変更された行は表示されません。コンボボックスの選択を変更しても、リストボックスの行は選択されません。私は努力し続け、私がそれを理解するなら私は私の解決策を掲示します。ありがとうございました。 – user657527

+0

@ user657527なぜあなたが 'EventArgument'を渡しているのか分かりますか? – Ankesh

関連する問題