我正在尝试通过XAML绑定将ObservableCollection<object>
传递给UserControl
。我的MainWindow
类生成Student
的随机列表。该列表已正确生成,然后初始化了组件。在我的XAML文件中,我绑定了学生,但是UserControl
类似乎没有任何数据。
public partial class MainWindow : Window
{
private ObservableCollection<object> _Students;
public ObservableCollection<object> Students { get => GetStudents(); }
public MainWindow()
{
GenerateStudentList();
Console.WriteLine("MainWindow: Students = {0}", Students.Count);
InitializeComponent();
}
private void GenerateStudentList()
{
for (int i = 0; i < 20; i++)
{
var s = Student.GenRandomStudent();
Console.WriteLine("Student: {0} - {1} {2}, {3}", s.StudentId, s.FirstName, s.LastName, s.Age);
Students.Add(s);
}
}
private ObservableCollection<object> GetStudents()
{
if (_Students == null)
_Students = new ObservableCollection<object>();
return _Students;
}
}
<Window x:Class="StudentApp.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:localCtr="clr-namespace:StudentApp.Controls"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DockPanel>
<Menu VerticalAlignment="Top" DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Abot"/>
<Separator/>
<MenuItem Header="_Exit"/>
</MenuItem>
</Menu>
<StatusBar DockPanel.Dock="Bottom" Height="auto">
<StatusBarItem Content="Status Bar"/>
</StatusBar>
<Grid DockPanel.Dock="Left" MinWidth="250">
<localCtr:StudentListBox Students="{Binding Students}" ContentStringFormat="{}{0} Students"/>
</Grid>
<Grid DockPanel.Dock="Right">
<Label Content="{Binding Students.Count}"/>
</Grid>
</DockPanel>
</Grid>
</Window>
public partial class StudentListBox : UserControl
{
public ObservableCollection<Object> Students
{
get { return (ObservableCollection<Object>)GetValue(StudentsProperty) ; }
set { SetValue(StudentsProperty, value); }
}
// Generated with 'propdp'
public static readonly DependencyProperty StudentsProperty =
DependencyProperty.Register(
"Students",
typeof(ObservableCollection<Object>),
typeof(StudentListBox),
new PropertyMetadata(new ObservableCollection<Object>())
);
///
public StudentListBox()
{
Console.WriteLine("StudentListBox: Students = {0}", Students.Count);
InitializeComponent();
}
}
<UserControl x:Class="StudentApp.Controls.StudentListBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:StudentApp.Controls"
mc:Ignorable="d"
Name="StudentListBoxControl">
<Grid>
<Label Content="{Binding Students.Count, ElementName=StudentListBoxControl}"
ContentStringFormat="{}{0} Students"/>
</Grid>
</UserControl>
答案 0 :(得分:0)
我解决了我的问题!我忘了使用DataContext
:
public MainWindow()
{
GenerateStudentList();
this.DataContext = this; // <---- Right here!
InitializeComponent();
}