2017-03-11 7 views
0

私のコンテンツが私のDataGridの中に表示されない質問がありました。しかし、私がthis MSDN articleをつまずいて、新しい(空のWPFアプリで)自分の作成したウィンドウのXAMLコンテンツをそのまま貼り付けると、問題は解決されます。DataGridに内容は表示されません。ここで は私の窓のXAMLです:ご覧のとおりVisual Studioの問題:MSDN 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を使用)。何が問題を引き起こす可能性がありますか?

答えて

1

DataGridを正しくバインドしていません。あなたは、あなたのモデルを掲示ますが、一例として、そのコレクションにあなたのデータグリッドを新しいウィンドウでのコードビハインドでのObservableCollectionを作成およびバインドしようとしていない:

<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(); 
     } 
1

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

今あなたが表示されるはずです。

理想的

Grid, after codebehind

、これはないだろうコードビハインドで行う必要があります。

関連する問題