多重绑定 ComboBox.Text
我有一个用于选择屏幕分辨率 width x height 的组合框。例如 : 1024x768(标准) 800x699(标准) 1500x900(宽)
我有一个 MutliValueConverter 将分辨率字符串转换为视图模型宽度和高度成员(在转换器的 ConvertBack 方法中),但 Convert 方法仅返回 null。
在组合框的 xaml 中,
<ComboBox.Text>
<MultiBinding Converter="{StaticResource resolutionConverter}">
<Binding Path="GameWidth"/>
<Binding Path="GameHeight"/>
</MultiBinding>
</ComboBox.Text>
当我单击在组合框中选择一个值时,组合框将回退到不选择任何内容。这是为什么 ?
I have a combobox for selecting screen resolution width x height . For example :
1024x768 (Standard)
800x699 (Standard)
1500x900 (Wide)
I have a MutliValueConverter to convert the resolution string to a view model width and height member (in the ConvertBack method of the converter ) , but the Convert method just return null.
in the xaml for the combobox I have
<ComboBox.Text>
<MultiBinding Converter="{StaticResource resolutionConverter}">
<Binding Path="GameWidth"/>
<Binding Path="GameHeight"/>
</MultiBinding>
</ComboBox.Text>
When I click to select a value in the combobox , the combobox will fall back to selecting nothing . Why is that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不应该做类似的事情,通常您只需将
ItemsSource
设置为视图模型集合,并将DisplayMemberPath
设置为包含的属性的名称显示字符串,这应该是关于它的。所选项目将成为视图模型。如果视图模型没有可以绑定到
字典的显示字符串,那么您需要将DisplayMemberPath
设置为Key
并将SelectedValuePath
更改为Value
,则选定的虚拟机将位于SelectedValue
属性中。(为项目生成显示字符串的另一个选项是使用
ItemTemplate
。VM 当然应该具有可以在其中使用的合适属性。这与IsEditable 一起使用效果不佳不过)
I do not think you are supposed to do anything like that, normally you would just set the
ItemsSource
to your collection of viewmodels and theDisplayMemberPath
to the name of the property which holds the display string and that should be about it. The selected item will then be the view model.If the viewmodels have no display string you could bind to a dictionary of
<string,ViewModel>
, then you need to set theDisplayMemberPath
toKey
and theSelectedValuePath
toValue
, then the selected VM will be in theSelectedValue
property.(Another option to generate the displayed string for the items is using the
ItemTemplate
. the VM's should then of course have suitable properties which can be used in it. This will not work well together withIsEditable
though)