我使用ViewModel中的属性为组合框设置了绑定。但是当我在设备上调试应用程序时,组合框的内容只是每个索引的模型名称,如LC_Points.Model.Grade
。
要调试此问题,我已经完成了以下检查:
有谁知道如何修复此绑定问题?
包含列表的ViewModel设置如下:
namespace LC_Points.ViewModel
{
public class MainViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
//call methods to initilise list data
GetGradeTypes();
GetSubjectTypes();
}
public List<Grade> grades { get; set; }
public List<Grade> subjects { get; set; }
public void GetGradeTypes()
{
List<Grade> gradeList = new List<Grade>();
// Adding Grades to List
gradeList.Add(new Grade { grade = "A1" });
gradeList.Add(new Grade { grade = "A2" });
gradeList.Add(new Grade { grade = "B1" });
gradeList.Add(new Grade { grade = "B2" });
gradeList.Add(new Grade { grade = "B3" });
gradeList.Add(new Grade { grade = "C1" });
gradeList.Add(new Grade { grade = "C2" });
gradeList.Add(new Grade { grade = "C3" });
gradeList.Add(new Grade { grade = "D1" });
gradeList.Add(new Grade { grade = "D2" });
gradeList.Add(new Grade { grade = "D3" });
gradeList.Add(new Grade { grade = "E,F,NG" });
gradeList.Add(new Grade { grade = "Pass" });
gradeList.Add(new Grade { grade = "Merit" });
gradeList.Add(new Grade { grade = "Distinction" });
grades = gradeList;
}
public void GetSubjectTypes()
{
List<Grade> subjectList = new List<Grade>();
// Adding Subjects to List
subjectList.Add(new Grade { subject = "Accounting" });
subjectList.Add(new Grade { subject = "Agricultural Economics" });
subjectList.Add(new Grade { subject = "Agricultural Science" });
subjectList.Add(new Grade { subject = "Ancient Greek" });
subjectList.Add(new Grade { subject = "Applied Math" });
subjectList.Add(new Grade { subject = "Arabic" });
subjectList.Add(new Grade { subject = "Art" });
subjectList.Add(new Grade { subject = "Artistic & Creative Group" });
subjectList.Add(new Grade { subject = "Biology" });
subjectList.Add(new Grade { subject = "Business" });
subjectList.Add(new Grade { subject = "Business Group" });
subjectList.Add(new Grade { subject = "Chemistry" });
subjectList.Add(new Grade { subject = "Classical Studies" });
subjectList.Add(new Grade { subject = "Construction Studies" });
subjectList.Add(new Grade { subject = "Design & Comm Graphics" });
subjectList.Add(new Grade { subject = "Economics" });
subjectList.Add(new Grade { subject = "Engineering" });
subjectList.Add(new Grade { subject = "English" });
subjectList.Add(new Grade { subject = "French" });
subjectList.Add(new Grade { subject = "Geography" });
subjectList.Add(new Grade { subject = "German" });
subjectList.Add(new Grade { subject = "Hebrew Studies" });
subjectList.Add(new Grade { subject = "History" });
subjectList.Add(new Grade { subject = "Home Economics" });
subjectList.Add(new Grade { subject = "Irish" });
subjectList.Add(new Grade { subject = "Italian" });
subjectList.Add(new Grade { subject = "Japanese" });
subjectList.Add(new Grade { subject = "Languages & Humanities" });
subjectList.Add(new Grade { subject = "Latin" });
subjectList.Add(new Grade { subject = "Link Modules" });
subjectList.Add(new Grade { subject = "Mathematics" });
subjectList.Add(new Grade { subject = "Music" });
subjectList.Add(new Grade { subject = "Other Language" });
subjectList.Add(new Grade { subject = "Physics" });
subjectList.Add(new Grade { subject = "Physics & Chemistry" });
subjectList.Add(new Grade { subject = "Practical Group" });
subjectList.Add(new Grade { subject = "Religious Education" });
subjectList.Add(new Grade { subject = "Russian" });
subjectList.Add(new Grade { subject = "Science Group" });
subjectList.Add(new Grade { subject = "Social Group" });
subjectList.Add(new Grade { subject = "Spanish" });
subjectList.Add(new Grade { subject = "Technology" });
subjects = subjectList;
}
}
}
我在MainPage View中设置了这样的绑定:
<Page x:Class="LC_Points.MainPage"
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:local="using:LC_Points"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
DataContext="{Binding Source={StaticResource Locator}}"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40*" />
<RowDefinition Height="20*" />
<RowDefinition Height="30*" />
<RowDefinition Height="30*" />
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4.5*" />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ComboBox x:Name="subjectCmbBx"
Grid.Row="1"
Grid.ColumnSpan="2"
Width="174"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ItemsSource="{Binding subjects}" />
<ComboBox x:Name="gradeCmbBx"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Width="70"
HorizontalAlignment="Right"
ItemsSource="{Binding grades}" />
</Grid>
</Page>
调试期间的输出如下所示:
答案 0 :(得分:1)
除非您提供DisplayMemberPath
(https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.displaymemberpath%28v=vs.110%29.aspx),这是我建议您做的,否则它将使用提供的Grade::ToString()
实现。所以,您也可以覆盖它。
答案 1 :(得分:1)
您需要告诉ComboBox如何显示其项目。否则它只会在绑定项上调用ToString()并显示一些不可思议的东西。
有三种方法可以获得合理的输出:
在DataObject上定义合理的ToString()。这是一个好主意,即使你做其中一个,因为如果你没有另外设置辅助功能信息,讲述者会读这个。
在ComboBox上定义DisplayMemberPath以指向要显示的属性。
<ComboBox x:Name="subjectCmbBx"
Grid.Row="1"
Grid.ColumnSpan="2"
Width="174"
HorizontalAlignment="Left"
VerticalAlignment="Top"
DisplayMemberPath="Name"
ItemsSource="{Binding subjects}" />
<ComboBox x:Name="subjectCmbBx"
Grid.Row="1"
Grid.ColumnSpan="2"
Width="174"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ItemsSource="{Binding subjects}" >
<ComboBox.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="{Binding Picture}" Grid.Column="0" />
<TextBlock Text="{Binding Name}" Grid.Column="1" />
</Grid>
</DataTemplate>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<StaticResource ResourceKey="DataTemplate1"/>
</ComboBox.ItemTemplate>
</ComboBox>
我建议#1(作为辅助功能的最低要求)和大多数情况下的#3。