2015-05-04 85 views
0

由于某些原因,SelectedItem未设置为数据库中的字段。ComboBox中的SelectedItem未设置为数据字段

XAML:

  <!-- Type --> 
      <Label Grid.Column="0" Grid.Row="1" 
        Style="{StaticResource FormLabelStyle}" 
        Content="Type:"/> 
      <Border Grid.Column="1" Grid.Row="1" 
        Style="{StaticResource FormBorderStyle}" 
        Width="350"> 
       <ComboBox x:Name="codeType" Margin="5" Padding="0" 
          FontSize="20" FontFamily="Arial" BorderThickness="0" 
          BorderBrush="Transparent" Background="White" 
          Text="{Binding CodType}" SelectedItem="{Binding CodType}"> 
        <ComboBoxItem Content="C"/> 
        <ComboBoxItem Content="C++"/> 
        <ComboBoxItem Content="C#"/> 
        <ComboBoxItem Content="PL/SQL"/> 
        <ComboBoxItem Content="SQL"/> 
        <ComboBoxItem Content="HTML"/> 
        <ComboBoxItem Content="XAML"/> 
        <ComboBoxItem Content="Unix Shell Script"/> 
       </ComboBox> 
      </Border> 

代码背后:

public ChangeCode(CodeRecord codRec) 
{ 
    _codeRecord = codRec; 
    this.DataContext = _codeRecord; 

    InitializeComponent(); 
} 

当屏幕显示欲被选择的当前CodType字段。调试显示它确实不为null,并且是组合对象之一。组合框没有显示任何选择。我究竟做错了什么?

+0

你会介意分享CodeRecord的代码之前选择的价值? ChangeCode是我相信的窗口类吗? – Domysee

回答

0

当您创建ComboBoxItem时,内容文本是字符串,字符串类没有用于显示文本的属性,它可能允许您通过使用string.ToString()进行选择,但是当显示屏幕时,它不会'没有与ComboBox项目匹配的属性并显示SelectedValue中的内容。为了解决这个问题,必须如下创建一个新类ComboBoxItemString与属性的valueString

public class ComboBoxItemString 
{ 
    public string ValueString { get; set; } 
} 

然后创建在资源的ComboBoxItemString的阵列(CodeTypesList),如下所示,并将其绑定到的ItemsSource然后使用poperty的valueString中的DisplayMemberPath和SelectedValuePath

<Border Grid.Column="1" Grid.Row="1" 
     Style="{StaticResource FormBorderStyle}" 
     Width="350"> 
    <Border.Resources> 
     <x:Array x:Key="CodTypesList" Type="local:ComboBoxItemString"> 
      <local:ComboBoxItemString ValueString = "C"/> 
      <local:ComboBoxItemString ValueString = "C++"/> 
      <local:ComboBoxItemString ValueString = "C#"/> 
      <local:ComboBoxItemString ValueString = "PL/SQL"/> 
      <local:ComboBoxItemString ValueString = "SQL"/> 
      <local:ComboBoxItemString ValueString = "HTML"/> 
      <local:ComboBoxItemString ValueString = "XAML"/> 
      <local:ComboBoxItemString ValueString = "Unix Shell Script"/> 
     </x:Array> 
    </Border.Resources> 
    <ComboBox x:Name="codeType" Margin="5" Padding="0" 
       FontSize="20" FontFamily="Arial" BorderThickness="0" 
       BorderBrush="Transparent" Background="White" 
       SelectedValue="{Binding CodType}" 
       ItemsSource="{StaticResource CodTypesList}" 
       DisplayMemberPath="ValueString" SelectedValuePath="ValueString" /> 
</Border> 

现在,当你来到这个画面,应该使用属性显示在ComboBox

+0

我已经实施(几乎)您的解决方案。但是,我无法获得编译的更改。我得到一个错误,“类型引用找不到一个名为'ComboBoxItemString'的公共类型,这是该类的c#代码。组; } } }“既然它在命名空间中,我不明白它为什么看不到它。如何在注释中设置代码格式? – llihttocs

+0

您需要将项目的XML名称空间添加到XAML中,然后将其将会看到类型引用'这里是我添加'xmlns :local =“clr-namespace:ws”',当你引用ComboBoxItemString数组''local:ComboBoxItemString ValueString =“C”/>''时,请确保使用相同的前缀'local'' – Ash

+0

我已经更新了解决方案,本地'前缀代替'e' – Ash

相关问题