我知道标题听起来可能令人困惑,但我不确定问题到底在哪里。但让我们从头开始。
我有一个简单的类
public class LessonListModel
{
public string Lesson_Title { get; set; }
public string Lesson_Parameter { get; set; }
}
下面是一个方法,它将SQL查询存储在上面类的通用对象列表中
public List<LessonListModel> Obtain_Lesson_List(int Id, string Language)
{
List<LessonListModel> LessonLista = new List<LessonListModel>();
LessonListModel lessonListModel = new LessonListModel();
using (var connection = GetConnection())
using (var command = new SqlCommand())
{
connection.Open();
command.Connection = connection;
command.CommandText = "select Lesson_Title, Lesson_Parameter from [Lesson] where Id_Course in (Select Id from [Course] where [Course_Name] = @language)";
command.Parameters.Add("@language", SqlDbType.NVarChar).Value = Language;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
lessonListModel.Lesson_Title = reader["Lesson_Title"].ToString();
Console.WriteLine(lessonListModel.Lesson_Title);
lessonListModel.Lesson_Parameter = reader["Lesson_Parameter"].ToString();
Console.WriteLine(lessonListModel.Lesson_Parameter);
LessonLista.Add(lessonListModel);
}
LessonLista.ForEach(x => Console.WriteLine("{0}\t", x));
}
return LessonLista;
}
}
这就是麻烦已经开始的地方。我不明白为什么,但我所有的元素都显示的控制台。WriteLine作为ApkaJezykowa.MVVM.Model.LessonListModel
即使元素被正确地查询。我真的不知道是什么原因,为什么数据没有正确地添加到列表,或者可能只是我不能正确地显示它。
下面是XAML:
<ListView x:Name="LV"
Background="#484848"
BorderBrush="Transparent"
ItemsSource="{Binding LessonsList}"
ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
VerticalAlignment="Stretch" SizeChanged="LV_SizeChanged">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn>
<DataTemplate>
<StackPanel Orientation="Horizontal"
Width="230"
Height="40">
<Button
Background="Transparent"
Height="40"
Width="230"
HorizontalAlignment="Right"
BorderThickness="0"
Margin="0,0,0,0"
Command="{Binding FrenchLessonUpdateViewCommand}"
CommandParameter="{Binding Lesson_Parameter}">
<TextBlock Text="{Binding Lesson_Title}"
Margin="0,0,0,0"
Style="{StaticResource Font_Style}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="Black"/>
</Button>
</StackPanel>
</DataTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
//It was just ItemContainerStyle part here, nothing important
</ListView>
您可以猜到,它以与Console中相同的方式显示元素。Writeline(),而不是它应该是什么样子。
这里也是ViewModel,只是缩短了重要的部分:
public class FrenchLessonViewModel : BaseViewModel
{
public List<LessonListModel> lessonsList;
private BaseViewModel _selectedViewModel;
private ILessonRepository lessonRepository;
public List<LessonListModel> LessonsList { get { return lessonsList; } set { lessonsList = value; OnPropertyChanged(nameof(LessonsList)); } }
public ICommand FrenchLessonUpdateViewCommand { get; set; }
public FrenchLessonViewModel()
{
lessonRepository = new LessonRepository();
LoadLesson();
}
public void LoadLesson()
{
if (ExerciseLevelModel.Instance.Level != 0 && ExerciseLevelModel.Instance.Language != null)
{
LessonsList = lessonRepository.Obtain_Lesson_List(lesson.Id, ExerciseLevelModel.Instance.Language);
LessonsList.ForEach(x => Console.WriteLine("{0}\t",x));
}
}
}
我已经尝试过多次重写这个方法,包括从这里发布的问题中,结果都是一样的。对于XAML,我真的不知道我在哪里犯了错误。
1条答案
按热度按时间brc7rcf01#
好吧,事实证明,控制台的执行。写行指令执行得很差,元素被正确添加。
foreach(LessonListModel p in LessonsList){ Console.WriteLine(p.Lesson_Title,p._Parameter);}
这是正确的方法。
对于XAML本身,我只是删除了与网格相关的元素并替换了listview。使用listview查看。itemtemplate.