【问题标题】:Visual Studio issue: MSDN XAML code doesn't workVisual Studio 问题:MSDN XAML 代码不起作用
【发布时间】:2017-03-11 16:06:57
【问题描述】:

我问了一个问题,我不能让我的内容出现在我的 DataGrid 中,然后我想我尝试了一个可行的解决方案并从那里开始。但是,当我偶然发现this MSDN article 并将其 XAML 内容复制粘贴到我新创建的窗口中(在我新的空 WPF 应用程序中)时,问题仍然存在:DataGrid 中没有出现任何内容。 这是我的 Window 的 XAML:

<Window x:Class="DataGridTry.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:local="clr-namespace:DataGridTry"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.Resources>
        <!--DataTemplate for Published Date column defined in Grid.Resources.  PublishDate is a property on the ItemsSource of type DateTime -->
        <DataTemplate x:Key="DateTemplate" >
            <StackPanel Width="20" Height="30">
                <Border Background="LightBlue" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate, StringFormat={}{0:MMM}}" FontSize="8" HorizontalAlignment="Center" />
                </Border>
                <Border Background="White" BorderBrush="Black" BorderThickness="1">
                    <TextBlock Text="{Binding PublishDate, StringFormat={}{0:yyyy}}" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" />
                </Border>
            </StackPanel>
        </DataTemplate>
        <!--DataTemplate for the Published Date column when in edit mode. -->
        <DataTemplate x:Key="EditingDateTemplate">
            <DatePicker SelectedDate="{Binding PublishDate}"  />
        </DataTemplate>
    </Grid.Resources>
    <DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <!--Custom column that shows the published date-->
            <DataGridTemplateColumn Header="Publish Date" CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

如您所见,只是复制粘贴,但仍然没有给出预期的结果。我要重新安装VS吗? (使用 2015 ATM)。什么可能导致问题?

【问题讨论】:

    标签: c# .net wpf visual-studio


    【解决方案1】:

    您没有正确绑定 DataGrid。您尚未发布模型,但作为示例,请尝试在新窗口的代码隐藏中创建 ObservableCollection 并将您的 DataGrid 绑定到该集合:

        <Window x:Class="DataGridTry.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:local="clr-namespace:DataGridTry"
        mc:Ignorable="d" Name="Root"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <!--DataTemplate for Published Date column defined in Grid.Resources.  PublishDate is a property on the ItemsSource of type DateTime -->
            <DataTemplate x:Key="DateTemplate" >
                <StackPanel Width="20" Height="30">
                    <Border Background="LightBlue" BorderBrush="Black" BorderThickness="1">
                        <TextBlock Text="{Binding PublishDate, StringFormat={}{0:MMM}}" FontSize="8" HorizontalAlignment="Center" />
                    </Border>
                    <Border Background="White" BorderBrush="Black" BorderThickness="1">
                        <TextBlock Text="{Binding PublishDate, StringFormat={}{0:yyyy}}" FontSize="8" FontWeight="Bold" HorizontalAlignment="Center" />
                    </Border>
                </StackPanel>
            </DataTemplate>
            <!--DataTemplate for the Published Date column when in edit mode. -->
            <DataTemplate x:Key="EditingDateTemplate">
                <DatePicker SelectedDate="{Binding PublishDate}"  />
            </DataTemplate>
        </Grid.Resources>
        <DataGrid DataContext="{Binding ElementName=Root}" Name="DG1" ItemsSource="{Binding PublishedDateModels}" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <!--Custom column that shows the published date-->
                <DataGridTemplateColumn Header="Publish Date" CellTemplate="{StaticResource DateTemplate}" CellEditingTemplate="{StaticResource EditingDateTemplate}" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
    

    示例模型:

    public class PublishedDateModel : INotifyPropertyChanged
        {
            private DateTime _publishDate;
    
            public DateTime PublishDate
            {
                get { return _publishDate; }
                set
                {
                    if (value.Equals(_publishDate)) return;
                    _publishDate = value;
                    OnPropertyChanged();
                }
            }
    
            public PublishedDateModel(DateTime date)
            {
                PublishDate = date;
            }
    
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            [NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    

    在您的 MainWindow.cs 文件中:

    public ObservableCollection<PublishedDateModel> PublishedDateModels { get; set; }
    
            public MainWindow()
            {
                PublishedDateModels = new ObservableCollection<PublishedDateModel>(new[]
                {
                    new PublishedDateModel(DateTime.Now), 
                    new PublishedDateModel(DateTime.Now.AddDays(1)), 
                    new PublishedDateModel(DateTime.Now.AddDays(-1)), 
                });
                InitializeComponent();
            }
    

    【讨论】:

      【解决方案2】:

      XAML 没有任何问题。只是不完整。

       Name="DG1" ItemsSource="{Binding}"
      

      DataGrid DG1 需要绑定数据。

      举个简单的例子,将其添加到代码隐藏 (MainWindow.xaml.cs):

      using System.Collections.ObjectModel;
      
      namespace DataGridTry
      {
          public class ThingBeingPublished
          {
              public DateTime PublishDate { get; set; }
          };
      
          public partial class MainWindow : Window
          {
              public MainWindow()
              {
                  InitializeComponent();
      
                  var listOfThings = new ObservableCollection<ThingBeingPublished();
      
                  listOfThings.Add(new ThingBeingPublished() { PublishDate = DateTime.Now.AddDays(-2) });
                  listOfThings.Add(new ThingBeingPublished() { PublishDate = DateTime.Now.AddDays(-1) });
                  listOfThings.Add(new ThingBeingPublished() { PublishDate = DateTime.Now });
      
                  DG1.ItemsSource = listOfThings;
              }
          }
      }
      

      现在你应该看到:

      理想情况下,这不会在代码隐藏中完成。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-15
        • 2017-07-11
        • 1970-01-01
        • 1970-01-01
        • 2019-06-23
        • 2017-10-02
        • 1970-01-01
        相关资源
        最近更新 更多