【问题标题】:How to bind the SelectedValue property of a ComboBox to a string property?如何将 ComboBox 的 SelectedValue 属性绑定到字符串属性?
【发布时间】:2011-07-13 13:13:09
【问题描述】:

我有一个来自 Silverlight Toolkit 的 DataForm 控件,其中有一个 ComboBox 字段。组合框的选定值应绑定到我的 IEditable 类的属性之一,以便在例如“H.S.”时被选中,我的属性值变为 H.S.但是,绑定将我的属性值更改为 System.Windows.Controls.ComboBoxItem。如果我改用 SelectedItem 属性,结果是相同的,正如 stackoverflow 上的一些其他类似问题所建议的那样。我应该如何将 ComboBox 的 SelectedValue 属性绑定到我的其他属性?这是我的 XAML(ItemSource 在 C# 代码中页面的构造函数中设置):

 <toolkit:DataForm  
                    Name="dataForm" 
                    ItemsSource="{Binding}"  
                    AutoGenerateFields="False"
                    Margin="150, 0, 150, 0"
                    CommitButtonContent="Next"
                    CancelButtonContent="Clear"
                    CommandButtonsVisibility="Commit, Cancel">
                    <StackPanel>
                         <toolkit:DataField LabelPosition="Top">
                            <ComboBox SelectedValue="{Binding Degree, Mode=TwoWay}">
                                <ComboBoxItem Content="H.S." />
                                <ComboBoxItem Content="B.S./B.A." />
                                <ComboBoxItem Content="M.S./M.A." />
                                <ComboBoxItem Content="Ph. D." />
                                <ComboBoxItem Content="M.D." />
                            </ComboBox>
                        </toolkit:DataField>
                    </StackPanel>
</toolkit:DataForm>

提前致谢。

【问题讨论】:

    标签: c# silverlight-4.0 binding silverlight-toolkit


    【解决方案1】:

    查看这篇文章,了解所有 ComboBox 控件属性的详细说明:http://johnpapa.net/binding-to-silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath

    尝试将 SelectedValuePath 属性设置为内容,例如:

    <ComboBox SelectedValue="{Binding Degree, Mode=TwoWay}" SelectedValuePath="Content">     
     <ComboBoxItem Content="H.S." />
     <ComboBoxItem Content="B.S./B.A." />
     <ComboBoxItem Content="M.S./M.A." />  
     <ComboBoxItem Content="Ph. D." /> 
     <ComboBoxItem Content="M.D." />
    </ComboBox> 
    

    【讨论】:

      猜你喜欢
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 2017-04-23
      • 1970-01-01
      • 2016-09-25
      • 2015-03-14
      • 1970-01-01
      相关资源
      最近更新 更多