【问题标题】:Why is ItemsControl in the following WPF XAML not showing anything?为什么以下 WPF XAML 中的 ItemsControl 没有显示任何内容?
【发布时间】:2014-12-30 04:42:05
【问题描述】:

当我运行应用程序时,我希望看到 5 个按钮(请参阅下面我的 XAML 中的 ItemsControl\DataTemplate\Button),每个按钮的内容都类似于“55/42”,表示最高最低温度。但是,窗口是空白的。我知道这与 ItemsControl 有关,因为我可以在不使用 ItemsControl 的情况下显示数据。有人能看出我的错误吗?

<Window x:Class="Embed_WeatherSummaryAsItemsControl_ToMain.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    FocusManager.FocusedElement="{Binding ElementName=InputCity}"
    Title="Weather App" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBox x:Name="InputCity" Grid.Row="0" Width="200" Text="{Binding CityAndOptionalCountry}"></TextBox>
    <Button Grid.Row="0" Width="50" Content="Go" Margin="260,0,0,0" Command="{Binding GetWeatherReportCommand}"></Button>
    <ItemsControl Grid.Row="1" ItemsSource="{Binding WeatherForecastSummaryCollection}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding Path=MaxMinTemperature}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>          
    </ItemsControl>
</Grid>

如下图,“WeatherForecastSummaryCollection”是ViewModel类的集合属性,“MaxMinTemperature”是集合中item的属性。

public class MainWindowViewModel : ViewModelBase
{
    ....

    private List<WeatherForecastSummary> mWeatherForecastSummaryCollection;
    public List<WeatherForecastSummary> WeatherForecastSummaryCollection
    {
        get { return mWeatherForecastSummaryCollection; }
        set
        {
            mWeatherForecastSummaryCollection = value;
            OnPropertyChanged("WeatherForecastSummaryCollection");
        }
    }
    .....
}

public class WeatherForecastSummary
{
    public string MaxMinTemperature { get; set; }
}

感谢您的帮助!

【问题讨论】:

  • 如何填充集合并将控件绑定到它?
  • 你能展示你实际实例化集合并添加5个项目的代码吗?
  • 我猜你首先将一个空列表分配给WeatherForecastSummaryCollection,然后添加5个项目(这是标准方式)。但列表不会发送有关内容更改的通知。将类型 List&lt;WeatherForecastSummary&gt; 更改为 ObservableCollection&lt;WeatherForecastSummary&gt; 会有所帮助。
  • 是的,就是这样 - 我忘了让列表可见。谢谢gomi42!!

标签: wpf xaml mvvm itemscontrol


【解决方案1】:

我认为您在这里缺少 DataContext。 即使你使用的是 View 的代码隐藏,你也需要编写

this.DataContext = this;

或者,如果您使用其他类作为视图模型,您可能希望将数据上下文指向该类。只需将上面代码中的“this”替换为您的 viewmodels 对象即可。 在这种情况下,

this.DataContext = new MainWindowViewModel();

【讨论】:

  • App xaml 代码隐藏确实具有该数据上下文集。我添加了一个方法来覆盖 App 的 OnStartup,这是我实例化 MainWindow 和 MainWindowVM 的地方,将 MainWindow 的 DataContext 设置为 MainWindowVM,最后执行 MainWindow.Show()。我只是对 ItemsControl 有问题。我可以毫无问题地绑定到另一个单按钮控件,所以我知道 DataContext 不是问题,除非我需要为 ItemsControl 再次单独设置 DataContext。
  • 我不确定你为什么要这样做??可以将上面显示的那段代码移动到要绑定到 MainWindowViewModel 的 Page 的构造函数中。
  • Aditya,没有使用 MVVM 设计模式修改视图、页面或窗口代码隐藏。您的建议不会解决问题,因为罪魁祸首是上面 gomi42 指出的“可观察”集合!代码现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多