【问题标题】:SelectedItem in ComboBox not being set to data fieldComboBox 中的 SelectedItem 未设置为数据字段
【发布时间】:2015-05-04 15:37:11
【问题描述】:

由于某种原因,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 字段。调试显示它确实不为空,并且是组合框项之一。组合框显示未选择任何内容。我做错了什么?

【问题讨论】:

  • 您介意分享 CodeRecord 的代码吗? ChangeCode 是我相信的窗口类?

标签: c# xaml combobox wpf-controls


【解决方案1】:

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

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

然后如下图在资源中创建一个ComboBoxItemString的数组(CodeTypesList)并绑定到ItemsSource,然后在DisplayMemberPath和SelectedValuePath中使用poperty ValueString

<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 中显示先前选择的值

【讨论】:

  • 我已经(几乎)实现了您的解决方案。但是,我无法编译更改。我收到一个错误,“类型引用找不到名为'ComboBoxItemString'的公共类型。这是该类的c#代码。“命名空间ws { public class ComboBoxItemString { public string ValueString { get;放; } } }" 既然是在命名空间里,我不明白为什么看不到。在cmets中如何格式化代码?
  • 您需要将项目的 XML 命名空间添加到 XAML,然后它会看到类型引用 &lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ws" Title="MainWindow" Height="350" Width="525"&gt; 这是我添加 xmlns:local="clr-namespace:ws" 的地方,请确保使用相同的前缀'local' 当你引用 ComboBoxItemString 数组 &lt;local:ComboBoxItemString ValueString="C"/&gt;
  • 要格式化 cmets 中的代码,请使用重音/反引号字符“您的代码”。它是 tab 键上方的键,数字 1 和 ! 左边的键输入US keyboard layout
  • 好的。我让它编译,但当我进入屏幕时,组合框仍然没有显示任何选择的内容。是否需要在 get 和 set 属性中添加代码?
  • 使用SelectedValue="{Binding CodType}" 而不是SelectedItem
猜你喜欢
  • 2023-03-09
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
相关资源
最近更新 更多