WPF ItemsControl 未呈现 UI
WPF ItemsControl is not rendering UI
这是循环字符串数组的 UI 代码:
MainWindow.xaml
<ItemsControl ItemsSource="{Binding sideMenuCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="2" BorderBrush="Black" >
<StackPanel Orientation="Horizontal" Height="50">
<TextBlock Text="" Margin="10,0,0,0" FontFamily="{StaticResource FontAwesome}" FontSize="35" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="{Binding}" FontSize="26" Margin="10,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Name="smb_dashboard" MouseDown="smb_dashboard_MouseDown"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
MainWindow.xaml.cs
public List<string> sideMenuCollection = new List<string> {
"Dashboard",
"Customers",
"Items",
"Reports"
};
不知道这里出了什么问题?
应该是在红色区域创建侧边菜单。
为了制作
ItemsSource="{Binding sideMenuCollection}"
工作,sideMenuCollection
必须是属性,不是字段,Window的DataContext
应该设置为Window实例,拥有 属性:
public partial class MainWindow : Window
{
public List<string> sideMenuCollection { get; } = new List<string>
{
"Dashboard",
"Customers",
"Items",
"Reports"
};
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
这是循环字符串数组的 UI 代码: MainWindow.xaml
<ItemsControl ItemsSource="{Binding sideMenuCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="2" BorderBrush="Black" >
<StackPanel Orientation="Horizontal" Height="50">
<TextBlock Text="" Margin="10,0,0,0" FontFamily="{StaticResource FontAwesome}" FontSize="35" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="{Binding}" FontSize="26" Margin="10,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Name="smb_dashboard" MouseDown="smb_dashboard_MouseDown"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
MainWindow.xaml.cs
public List<string> sideMenuCollection = new List<string> {
"Dashboard",
"Customers",
"Items",
"Reports"
};
不知道这里出了什么问题?
应该是在红色区域创建侧边菜单。
为了制作
ItemsSource="{Binding sideMenuCollection}"
工作,sideMenuCollection
必须是属性,不是字段,Window的DataContext
应该设置为Window实例,拥有 属性:
public partial class MainWindow : Window
{
public List<string> sideMenuCollection { get; } = new List<string>
{
"Dashboard",
"Customers",
"Items",
"Reports"
};
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}