C# ComboBox打印对象类型
我正在努力学习WPF以及最重要的MVVM风格 我有一个简单的实践应用程序,我想在一个组合框中显示代码 我的代码C# ComboBox打印对象类型,c#,wpf,C#,Wpf,我正在努力学习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.W
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间流代码
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
私有可观测收集代码;
公共主窗口()
{
初始化组件();
代码=新的ObservableCollection()
{
新代码(){CodeID=“1”,Code=“CODETEXT”}
};
steamCode.ItemsSource=codes.ToString();
}
}
公共类代码
{
公共字符串代码ID{get;set;}
公共字符串代码{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>
目前,我的组合框正在运行,因为组合框中的每个选项都是“System.Collections.Objectmodel.ObservableCollection`1[SteamCodes.Codes]”行中的一个字母
每个字母在组合框中都是不同的下拉选项
任何我出错的想法。您的
组合框
ItemSource
必须是项目的集合,而不是字符串:
steamCode.ItemsSource = codes;
您还必须通过设置DisplayMemberPath
property来指定项目的哪个属性必须被视为显示在组合框中的值:
steamCode.DisplayMemberPath = "Code";
steamCode.SelectedValuePath = "CodeID";
要指定绑定对象的哪个属性将用作实际选定值,必须使用SelectedValuePath
属性:
steamCode.DisplayMemberPath = "Code";
steamCode.SelectedValuePath = "CodeID";
MVVM方法是这样的:
public class ViewModel
{
public ObservableCollection<Codes> Codes { get; }
= new ObservableCollection<Codes>();
}
XAML:
<ComboBox ItemsSource="{Binding Codes}" DisplayMemberPath="Code" .../>
您的
组合框必须是项的集合,而不是字符串:
steamCode.ItemsSource = codes;
您还必须通过设置DisplayMemberPath
property来指定项目的哪个属性必须被视为显示在组合框中的值:
steamCode.DisplayMemberPath = "Code";
steamCode.SelectedValuePath = "CodeID";
要指定绑定对象的哪个属性将用作实际选定值,必须使用SelectedValuePath
属性:
steamCode.DisplayMemberPath = "Code";
steamCode.SelectedValuePath = "CodeID";
MVVM方法是这样的:
public class ViewModel
{
public ObservableCollection<Codes> Codes { get; }
= new ObservableCollection<Codes>();
}
XAML:
<ComboBox ItemsSource="{Binding Codes}" DisplayMemberPath="Code" .../>
itemsource
应绑定到数据项的集合。您正在分配(而不是绑定)一个字符串,该字符串是字符的集合。XAML中的绑定表达式不起作用。请在此处阅读基本内容:。好的,谢谢,我现在将查看该链接。它还应该做什么?你正在把你的整个收藏整理得井井有条。如果项的源设置为两个值-->CodeID和CodeItemsSource
应绑定到数据项集合,则组合框应如何决定显示哪一个。您正在分配(而不是绑定)一个字符串,该字符串是字符的集合。XAML中的绑定表达式不起作用。请在此处阅读基本内容:。好的,谢谢,我现在将查看该链接。它还应该做什么?你正在把你的整个收藏整理得井井有条。如果项目的源设置为两个值-->CodeID和CODETH,那么组合框应该如何决定显示哪一个呢?这两个值非常棒,非常有用,XAML中的绑定在哪里,因为我认为这是MVVM的工作方式?如果我分配steamCode.ItemsSource=codes;那么在代码中没有必要绑定吗?非常感谢你的回答!是的,您不需要在XAML中设置ItemsSource,因为您已经在代码隐藏中完成了它,这是实现这一点的一种方法。看看这篇文章,更好地了解WPF中的数据绑定是如何工作的:本教程也是一个很好的开始:这很好,非常有用,但是XAML中的绑定在哪里,因为我认为这是MVVM的工作方式?如果我分配steamCode.ItemsSource=codes;那么在代码中没有必要绑定吗?非常感谢你的回答!是的,您不需要在XAML中设置ItemsSource,因为您已经在代码隐藏中完成了它,这是实现这一点的一种方法。请阅读本文,更好地了解WPF中的数据绑定工作原理:本教程也是一个良好的开端: