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>
ではありませんあなたのヒントを事前に感謝は
ヴィンセント
Wowwのおかげ:ウィンドウ自体に -
は、だからここで実際に見にそれを伝えます。あなたのコメントで、私のサンプルは今は正常に動作しています。私はより良い理解を持っています。さて、私はコードの背後にあるバインディングのサンプルが本当に正しいのかどうかは疑問です。ありがとうございました! –
@VincentMontagnonまあ、 "正しい"、私は駄目です。それがうまくいけばそれは働いた。しかし、MVVM viewmodel/XAMLバインディングのアプローチは、長い目で見れば、学習曲線は急峻ですが、はるかに強力で柔軟性があります。 –
@Ed ...私はそれに同意します。どうもありがとう。 –