我有一个在应用程序启动时定义了DataContext的ComboBox,指向适当的ViewModel。我希望从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被适当地设置为视图窗口的DataContext。我做错了什么?
发布于 2013-01-24 06:57:49
您正在使用;
SelectionChanged="{Binding Path=DataContext.cbConnection_SelectionChanged}" 这实际上是一个事件。
您应该将ViewModel中的公共属性(可能实现INotifyPropertyChanged)绑定到SelectedItem属性,以管理对选定内容所做的更改。
假设您的窗口具有DataContext,而不是combobox本身...
SelectedItem Binding版本:
所以你的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文本绑定版本:
当然,如果您所感兴趣的只是他们所选择的文本值,那么理论上您可以将ComboBox Text属性绑定到ViewModel中的公共字符串属性;
那么你的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 PropertySelectedValue Binding版本:
如果要显示键,但需要键/值对中的值,则应绑定到SelectedValue;
XAML;
<ComboBox x:Name="cbConnection"
ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
DisplayMemberPath="@Key"
SelectedValuePath="@Value"
SelectedValue="{Binding Path=DataContext.cbConnectionValue}" />ViewModel;
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注意额外的@符号。
如上所述,这假设您的窗口在此处设置了DataContext。如果没有,那么从上面的绑定中删除“DataContext.”!
我想你现在看到的是ComboBox中列出的项目吧?
希望这能有所帮助!
发布于 2013-01-24 19:46:43
你必须为comboBox选择使用事件触发器改变事件你应该尝试下面提到的代码
<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" https://stackoverflow.com/questions/14490914
复制相似问题