2017-02-28 7 views
1

WPFとバインディングをテストしています。 コードビハインドで行われたバインディングでうまく動作しているデモがあります。Datagrid XAMLでのDataGridバインディングとコードバインドのバインド

は、ここに私のXAMLコードとC#のコードです。

<Window x:Class="WpfApplication1.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:WpfApplication1" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 

<Grid> 

    <DataGrid x:Name="dgrdMaGrid" /> 
    <Button x:Name="button" Content="Quitter" HorizontalAlignment="Left" Margin="355,268,0,0" VerticalAlignment="Top" Width="155" Height="44" Click="button_Click"/> 

</Grid> 

のC#:

public partial class MainWindow : Window 
{ 
    public ObservableCollection<Individu> listeIndividus = new ObservableCollection<Individu>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 

     // Load data to display 
     listeIndividus = loadDummyData(); 

     // Build the binding 
     dgrdMaGrid.DataContext = this; 
     dgrdMaGrid.ItemsSource = listeIndividus; 

    } 

私は同じ結果を持っていますが、XAML内の結合を行うことによってために私のXAMLコードを変更する方法を疑問に思って。

私はのItemsSource = "{バインディングlisteIndividusを}" を使用する必要があることを知っているが、それが十分...

<Window x:Class="WpfApplication1.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:WpfApplication1" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525"> 

<Grid> 

    <DataGrid x:Name="dgrdMaGrid" ItemsSource="{Binding listeIndividus}"/> 
    <Button x:Name="button" Content="Quitter" HorizontalAlignment="Left" Margin="355,268,0,0" VerticalAlignment="Top" Width="155" Height="44" Click="button_Click"/> 

</Grid> 

ではありませんあなたのヒントを事前に感謝は

ヴィンセント

答えて

0

"//バインディングをビルドする"とは、バインディングではありません。 Bindingクラスのインスタンスを作成していません。それは単なる割り当てであり、その違いは重要です。

OK、今あなたのコードを修正するために、オーバー講義。

まず、あなただけのプロパティに、フィールドにバインドすることはできません。このBindingですが、のviewmodelを持っていないので、それがあるとして、それは動作しません、

public ObservableCollection<Individu> listeIndividus { get; } = new ObservableCollection<Individu>(); 

第二:だからlisteIndividusはゲッターを必要とします。このバインディングはlisteIndividusという名前のプロパティの場合はWindow.DataContextになります。 Window.DataContextはnullなので、見つからないでしょう。もしあなたがいたら、Window.DataContextはビューモデルになります。あなたのポストのための

<DataGrid 
    x:Name="dgrdMaGrid" 
    ItemsSource="{Binding listeIndividus, RelativeSource={RelativeSource AncestorType=Window}}" 
    /> 
+0

Wowwのおかげ:ウィンドウ自体に -

<DataGrid x:Name="dgrdMaGrid" ItemsSource="{Binding listeIndividus}"/> 

は、だからここで実際に見にそれを伝えます。あなたのコメントで、私のサンプルは今は正常に動作しています。私はより良い理解を持っています。さて、私はコードの背後にあるバインディングのサンプルが本当に正しいのかどうかは疑問です。ありがとうございました! –

+0

@VincentMontagnonまあ、 "正しい"、私は駄目です。それがうまくいけばそれは働いた。しかし、MVVM viewmodel/XAMLバインディングのアプローチは、長い目で見れば、学習曲線は急峻ですが、はるかに強力で柔軟性があります。 –

+0

@Ed ...私はそれに同意します。どうもありがとう。 –

関連する問題