选择的值不适用于WPF MVMVM中的组合框 [英] Selected valued not work with combo box in wpf mvvm

查看:76
本文介绍了选择的值不适用于WPF MVMVM中的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想在组合框中显示斜率值.我们的编码如下.

We want to show sleeted value in the combo box. We are coding as follows.

1-我已经创建了用于显示值的属性

1-I have created property for showing the value

private string _severityValue = "Select...";
        public string SeverityValue
        {
            get { return _severityValue; }
            set
            {
                SetProperty(ref this._severityValue, value);
                RaisePropertyChanged("SeverityValue");
            }
        }


xaml的设计如下

   <ComboBox Grid.Row="5" Grid.Column="0" Name="SeverityComboBox"  Width="150" Height="30" StaysOpenOnEdit="True" IsSynchronizedWithCurrentItem="True" 
                                     SelectedValue="{Binding RepairRecommendationsModel.SeverityValue, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top"  
                                       IsEditable="False" IsTextSearchCaseSensitive="False"  Margin="5,0,0,0" MaxHeight="30" MinHeight="30">
                                        <ComboBoxItem Content="Select..."/>
                                        <ComboBoxItem Content="Unit Down" />
                                        <ComboBoxItem Content="Possible Failure" />
                                        <ComboBoxItem Content="Preventative Maintenance" />
                                    </ComboBox>

当我们运行该应用程序时,它没有显示选定的值.我们无法确定问题.

When we run the application then it's not showing selected value. We are unable to determine the issue.

请帮助找出解决方案.谢谢

Please help to find out solution. thanks

推荐答案

您正在混合类型.您的ComboBox包含 ComboBoxItems ,但是您正在尝试绑定到 字符串属性.没有选择...".在ComboBox中可用的字符串将被选中.只有4个ComboBoxItems,因此运行时无法真正分辨要选择哪个项目.

You are mixing types. Your ComboBox contains ComboBoxItems but you are trying to bind to a string property. There is no "Select..." string available in the ComboBox to be selected. There are just 4 ComboBoxItems so the runtime can't really tell which item to be selected.

您应该做的是将字符串添加到ComboBox,并将Selected Item 属性绑定到您的源属性:

What you should do is to add strings to the ComboBox and bind the SelectedItem property to your source property:

<ComboBox Grid.Row="5" Grid.Column="0" Name="SeverityComboBox"  Width="150" Height="30" StaysOpenOnEdit="True" IsSynchronizedWithCurrentItem="True" 
                                     SelectedItem="{Binding RepairRecommendationsModel.SeverityValue, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top"  
                                       IsEditable="False" IsTextSearchCaseSensitive="False"  Margin="5,0,0,0" MaxHeight="30" MinHeight="30"
                  xmlns:s="clr-namespace:System;assembly=mscorlib">
            <s:String>Select...</s:String>
            <s:String>Unit Down</s:String>
            <s:String>Possible Failure</s:String>
            <s:String>Preventative Maintenance</s:String>
        </ComboBox> 

然后,只要对RepairRecommendationsModel.SeverityValue源属性的绑定有效,它将起作用.

Then it will work provided that the binding to the RepairRecommendationsModel.SeverityValue source property works.

请通过将有用的帖子标记为答案来结束您的话题: http://social.technet.microsoft.com/wiki/contents/articles/7359.forums-help-faq.aspx  

Please close your threads by marking helpful posts as answer: http://social.technet.microsoft.com/wiki/contents/articles/7359.forums-help-faq.aspx 


这篇关于选择的值不适用于WPF MVMVM中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆