wpf MVVM 数据绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14490914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVVM DataBinding
提问by sammarcow
I have a ComboBox with the DataContext defined at application start to the appropriate ViewModel. I want to grab items from a XML file but have user selections bind to the ViewModel, and ultimately the model.
我有一个 ComboBox,其中在应用程序启动时定义了 DataContext 到适当的 ViewModel。我想从 XML 文件中获取项目,但让用户选择绑定到 ViewModel,最终绑定到模型。
XAML:
XAML:
<ComboBox x:Name="cbConnection"
ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
DisplayMemberPath="Key"
SelectedValuePath="Value"
SelectionChanged="{Binding Path=DataContext.cbConnection_SelectionChanged}"
/>
but I am getting the following exception at runtime:
但我在运行时收到以下异常:
{"Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'."}
{"Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'."}
We know the ViewModel is appropriately set as the DataContext of the View's Window. What am I doing wrong?
我们知道 ViewModel 被适当地设置为视图窗口的 DataContext。我究竟做错了什么?
回答by PGallagher
You are using;
您正在使用;
SelectionChanged="{Binding Path=DataContext.cbConnection_SelectionChanged}"
Which is actually an event.
这实际上是一个事件。
You should bind a Public Property (possibly implementing INotifyPropertyChanged) in your ViewModel to the SelectedItemProperty to manage changes tn the Selection.
您应该将 ViewModel 中的公共属性(可能实现 INotifyPropertyChanged)绑定到SelectedItem属性以管理选择的更改。
Assuming that your window has the DataContext, rather than the combobox itself...
假设您的窗口具有 DataContext,而不是组合框本身...
SelectedItem Binding Version:
SelectedItem 绑定版本:
So your XAML would be something like;
所以你的 XAML 会是这样的;
<ComboBox x:Name="cbConnection"
ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
DisplayMemberPath="Key"
SelectedValuePath="Value"
SelectedItem="{Binding Path=DataContext.cbConnectionSelectedItem}"
/>
And in your ViewModel;
在你的 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
Text Binding Version:
文本绑定版本:
Of course, if all your interested in is the Text Value of what they've chosen, you could in theory just bind the ComboBox Text Property to a Public String Property in your ViewModel;
当然,如果您只对他们选择的文本值感兴趣,理论上您可以将 ComboBox 文本属性绑定到 ViewModel 中的公共字符串属性;
Your XAML would then be;
您的 XAML 将是;
<ComboBox x:Name="cbConnection"
ItemsSource="{Binding Source={StaticResource XmlConnectionList}, XPath=//ComboItem}"
DisplayMemberPath="Key"
SelectedValuePath="Value"
Text="{Binding Path=DataContext.cbConnectionText}"
/>
And your ViewModel;
还有你的 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 Binding Version:
SelectedValue 绑定版本:
If you are displaying the Key, but want the Value from the Key/Value Pair, then you should bind to the SelectedValue;
如果您正在显示键,但想要键/值对中的值,那么您应该绑定到 SelectedValue;
XAML;
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
Note the extra @ symbols.
注意额外的@ 符号。
As I mentioned above, this assumes that your Window has the DataContext set here. If not, then remove the "DataContext."'s from the Bindings above!
正如我上面提到的,这假设您的窗口在此处设置了 DataContext。如果没有,则从上面的绑定中删除“DataContext.”!
I assume you're seeing items listed in your ComboBox currently?
我假设您目前看到 ComboBox 中列出的项目?
Hope this helps!
希望这可以帮助!
回答by Dhaval Patel
you have to use event Triggers for comboBox Selection changed Event You should try below mention code
您必须使用事件触发器进行组合框选择更改事件您应该尝试下面提到的代码
<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>
for these you have to add reference like
对于这些你必须添加参考
xmlns:l="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

