绑定到 ComboBox 显示模型名称而不是 属性 内容
Binding to ComboBox displays Model name instead of property contents
我在 ViewModel 中使用 属性 为组合框设置了绑定。但是当我在设备上调试应用程序时,组合框的内容只是每个索引的型号名称,如 LC_Points.Model.Grade
.
为了调试这个问题,我进行了以下检查:
- 检查 属性 是 public
- 检查清单不为空
- 检查 VM 中的正确绑定名称
有谁知道如何解决这个绑定问题?
包含列表的 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 视图中设置了这样的绑定:
<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>
调试期间的输出如下所示:
除非您提供 DisplayMemberPath
(https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.displaymemberpath%28v=vs.110%29.aspx),这是我推荐的做法,否则它将使用提供的 Grade::ToString()
实现。因此,您也可以覆盖它。
您需要告诉 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 定义一个 ItemTemplate 以完全控制每个项目的显示方式。这将允许显示绑定对象的多个属性、格式以及比纯文本更好的 UI。您可以通过右键单击设计器中的 ComboBox 并选择编辑其他 Templates.Edit 生成的项目 (ItemTemplate)... 生成一个空模板,然后根据需要填写它:
<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。
我在 ViewModel 中使用 属性 为组合框设置了绑定。但是当我在设备上调试应用程序时,组合框的内容只是每个索引的型号名称,如 LC_Points.Model.Grade
.
为了调试这个问题,我进行了以下检查:
- 检查 属性 是 public
- 检查清单不为空
- 检查 VM 中的正确绑定名称
有谁知道如何解决这个绑定问题?
包含列表的 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 视图中设置了这样的绑定:
<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>
调试期间的输出如下所示:
除非您提供 DisplayMemberPath
(https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.displaymemberpath%28v=vs.110%29.aspx),这是我推荐的做法,否则它将使用提供的 Grade::ToString()
实现。因此,您也可以覆盖它。
您需要告诉 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 定义一个 ItemTemplate 以完全控制每个项目的显示方式。这将允许显示绑定对象的多个属性、格式以及比纯文本更好的 UI。您可以通过右键单击设计器中的 ComboBox 并选择编辑其他 Templates.Edit 生成的项目 (ItemTemplate)... 生成一个空模板,然后根据需要填写它:
<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。