【问题标题】:WPF ItemsControl is not rendering UI [duplicate]WPF ItemsControl 未呈现 UI [重复]
【发布时间】:2021-05-04 15:54:20
【问题描述】:

这是循环字符串数组的 UI 代码: MainWindow.xaml

<ItemsControl ItemsSource="{Binding sideMenuCollection}">
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Border BorderThickness="2" BorderBrush="Black" >
            <StackPanel Orientation="Horizontal" Height="50">
                <TextBlock Text="&#xf0e4;" 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"
        };

不知道这里出了什么问题?

它应该在红色区域创建sidemenus。

【问题讨论】:

  • 你如何设置那个窗口的数据上下文?
  • 我是初学者。我没有得到你的问题?
  • 嘿@AshishGehlot 等等。我想我已经为你找到了解决方案。请让我先检查一下。 :)
  • @AshishGehlot 请检查代码。它适用于 WPF 和 Silverligh.Best Wishes。 :)
  • 查看编辑后的答案,了解如何解决您的实际问题,即绑定无效。

标签: c# wpf data-binding datagrid wpf-controls


【解决方案1】:

为了制作

ItemsSource="{Binding sideMenuCollection}"

work,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;
    }
}

【讨论】:

  • 这是您期待的答案吗?
  • 是的@Linker,我正在学习 MVVM 教程,并且了解窗口表单,所以混合逻辑。
  • 太棒了。继续。
猜你喜欢
  • 2019-02-12
  • 1970-01-01
  • 2018-05-26
  • 1970-01-01
  • 2021-07-06
  • 2017-11-04
  • 2011-02-15
  • 2011-03-20
  • 2016-04-20
相关资源
最近更新 更多