【问题标题】:ComboBox printing the Object Type组合框打印对象类型
【发布时间】:2016-09-28 20:50:42
【问题描述】:

我正在尝试学习 WPF 以及最重要的 MVVM 做事风格。

我有一个简单的练习应用程序,我想在组合框中显示代码。

我的代码

    using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SteamCodes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<Codes> codes;
        public MainWindow()
        {
            InitializeComponent();

            codes = new ObservableCollection<Codes>()
            {
                new Codes() {CodeID = "1", Code="CODETEXT"}
            };
            steamCode.ItemsSource = codes.ToString();
        }
    }

    public class Codes
    {
        public string CodeID { get; set; }
        public string Code { get; set; }
    }
}

我的 XAML

<Window x:Class="SteamCodes.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SteamCodes"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="steamCode" ItemsSource="{Binding Source = Codes}" HorizontalAlignment="Left" Height="43" Margin="122,37,0,0" VerticalAlignment="Top" Width="259"/>
    </Grid>
</Window>

目前我的 Combo 框正在通过,因为 ComboBox 中的每个选项都是来自“System.Collections.Objectmodel.ObservableCollection`1[SteamCodes.Codes]”行中的一个字母

这些字母中的每一个都是组合框中的不同下拉选项。

我出错的任何想法。

【问题讨论】:

  • ItemsSource 应该绑定到数据项的集合。您正在分配(不绑定)一个字符串,它是字符的集合。 XAML 中的绑定表达式不起作用。在此处阅读基础知识:Data Binding Overview
  • 好的,谢谢,我现在看看那个链接。
  • 它还应该做什么?你正在把你的整个收藏放在一个字符串中。您必须编码的图像,如果项目的源设置为两个值,组合框应如何决定显示哪个 -> CodeID 和 Code

标签: c# wpf


【解决方案1】:

您的ComboBox ItemSource 必须是项目的集合,而不是字符串:

steamCode.ItemsSource = codes;

您还必须通过设置DisplayMemberPath 属性来指定必须将项目的哪个属性视为要在组合框中显示的值:

steamCode.DisplayMemberPath = "Code";

要指定绑定对象的哪个属性将用作实际选定值,您必须使用SelectedValuePath 属性:

steamCode.SelectedValuePath = "CodeID";

MVVM 方法是这样的:

public class ViewModel
{
    public ObservableCollection<Codes> Codes { get; }
        = new ObservableCollection<Codes>();
}

MainWindow 构造函数:

public MainWindow()
{
    InitializeComponent();

    var viewModel = new ViewModel();
    viewModel.Codes.Add(new Codes { CodeID = "1", Code = "CODETEXT" });

    DataContext = viewModel;
}

XAML:

<ComboBox ItemsSource="{Binding Codes}" DisplayMemberPath="Code" .../>

【讨论】:

  • 太棒了,工作得很好,但是 XAML 中的绑定在哪里出现,因为我认为这是 MVVM 做事的方式?如果我分配 steamCode.ItemsSource = 代码;在代码中那么有没有必要绑定?非常感谢您的回答!
  • 是的,您不需要在 XAML 中设置 ItemsSource,因为您已经在后面的代码中完成了它,这是执行此操作的一种方法。查看这篇文章,以更好地了解 WPF 中的 DataBinding 是如何工作的:codeproject.com/Articles/29054/WPF-Data-Binding-Part
  • 本教程也是一个好的开始:wpf-tutorial.com/data-binding/introduction
猜你喜欢
  • 2018-05-17
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 2019-03-22
相关资源
最近更新 更多