6

アプリケーションの開始時に適切なViewModelにDataContextが定義されたComboBoxがあります。XML ファイルから項目を取得したいのですが、ユーザーの選択内容を ViewModel にバインドし、最終的にはモデルにバインドします。

XAML:

<ComboBox x:Name="cbConnection"
          ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
          DisplayMemberPath="Key"
          SelectedValuePath="Value"
          SelectionChanged="{Binding Path=DataContext.cbConnection_SelectionChanged}"
          />

しかし、実行時に次の例外が発生します。

{"Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'."}

ViewModel が View の Window の DataContext として適切に設定されていることがわかっています。私は何を間違っていますか?

4

2 に答える 2

13

あなたが使用している;

SelectionChanged="{Binding Path=DataContext.cbConnection_SelectionChanged}" 

これは実際にイベントです。

選択の変更を管理するには、ViewModel のパブリック プロパティ (おそらく INotifyPropertyChanged を実装する) をSelectedItemプロパティにバインドする必要があります。

コンボボックス自体ではなく、ウィンドウに DataContext があると仮定すると...

SelectedItem バインディング バージョン:

したがって、XAML は次のようになります。

<ComboBox x:Name="cbConnection"
          ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
          DisplayMemberPath="Key"
          SelectedValuePath="Value"
          SelectedItem="{Binding Path=DataContext.cbConnectionSelectedItem}"
/>

そしてあなたのViewModelで;

Private _cbConnectionSelectedItem As XmlElement

Public Property cbConnectionSelectedItem As XmlElement
     Get
         Return _cbConnectionSelectedItem
     End Get
     Set(value As XmlElement)
         If value.Equals(_cbConnectionSelectedItem) = False Then
             _cbConnectionSelectedItem = value
             OnPropertyChanged("cbConnectionSelectedItem")
            End If
     End Set
End Property

テキストバインディングのバージョン:

もちろん、彼らが選択したテキスト値だけに関心がある場合は、理論的には、ViewModel で ComboBox Text プロパティを Public String プロパティにバインドするだけで済みます。

XAML は次のようになります。

<ComboBox x:Name="cbConnection"
              ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
              DisplayMemberPath="Key"
              SelectedValuePath="Value"
              Text="{Binding Path=DataContext.cbConnectionText}"
    />

そしてあなたのViewModel;

Private _cbConnectionText As String

Public Property cbConnectionText As String
     Get
         Return _cbConnectionText
     End Get
     Set(value As String)
         If value.Equals(_cbConnectionText) = False Then
             _cbConnectionText = value
             OnPropertyChanged("cbConnectionText")
            End If
     End Set
End Property

SelectedValue バインディング バージョン:

キーを表示しているが、キーと値のペアから値が必要な場合は、SelectedValue にバインドする必要があります。

XAML;

<ComboBox x:Name="cbConnection" 
    ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
    DisplayMemberPath="@Key" 
    SelectedValuePath="@Value" 
    SelectedValue="{Binding Path=DataContext.cbConnectionValue}" />

ビューモデル;

Private _cbConnectionValue As String

Public Property cbConnectionValue As String
     Get
         Return _cbConnectionValue
     End Get
     Set(value As String)
         If value.Equals(_cbConnectionText) = False Then
             _cbConnectionValue = value
             OnPropertyChanged("cbConnectionValue")
            End If
     End Set
End Property

余分な @ 記号に注意してください。

上で述べたように、これは Window に DataContext がここに設定されていることを前提としています。そうでない場合は、上記のバインディングから「DataContext.」を削除してください。

現在、ComboBox にリストされているアイテムが表示されていると思いますか?

お役に立てれば!

于 2013-01-23T22:57:49.100 に答える
7

コンボボックスの選択変更イベントにイベントトリガーを使用する必要があります。以下のコードを試してください。

<ComboBox Margin="192,5,5,5" DisplayMemberPath="AttachmentName" ItemsSource="{Binding AttachementList, Mode=TwoWay}" Style="{StaticResource BasicComboBoxStyle}" BorderThickness="2" BorderBrush="DarkGray"
                          Name="cmb_AttchDetails" Width="287" Height="25" SelectedItem="{Binding Defaultrequiredattachment, Mode=TwoWay}">
                    <l:Interaction.Triggers>
                        <l:EventTrigger EventName="SelectionChanged">
                            <l:InvokeCommandAction Command="{Binding DataContext.AttachmentNameCommand,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=controls:ChildWindow}}" CommandParameter="{Binding ElementName=cmb_AttchDetails,Path=SelectedItem}" />
                        </l:EventTrigger>
                    </l:Interaction.Triggers>
                </ComboBox>

これらについては、次のような参照を追加する必要があります

  xmlns:l="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
于 2013-01-24T11:46:43.130 に答える