2016-07-12 3 views
-3

私は文字列の観測可能なコレクションを含むオブジェクトを持っています。例えば他のオブジェクト内の文字列のObservableCollectionにDataGridをバインドします。

テイク:

public class Container 
{ 
    public ObservableCollection<string> strs; //This won't work, see edit! 
. 
. 
. 
} 

XAML:サーチャーのための

<DataGrid ItemsSource="{Binding Container}" AutoGenerateColumns="False" > 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Strings" Binding="{Binding}" /> 
    </DataGrid.Columns> 
</Datagrid> 

編集:いくつかは、上記のアプローチと間違ってありますが、まずあなたがアイテムのプロパティにバインドすることができます単にそれらのプロパティを参照するだけです。代わりに、それは性質(そのようだ見つけようとしての、第二

ItemsSource="{Binding Container.strs}" 

、文字列の内容が文字列のプロパティではありませんので、

Binding="{Binding}" 

バインド直接文字列に:この場合、長さとして)

最後に、フィールドにバインドすることはできません。プロパティのみ、違いは何ですか?

public ObservableCollection<string> strs; //This is a field 
public ObservableCollection<string> strs {get; set;} //This is property 

ボーナス:あなただけのSTRSをインスタンス化している場合は、一度、その後のObservableCollectionが通知されますそれが起こる以内/に変更しましたが、あなたはそれはないでしょうポインタを変更している場合は、この問題を解決する際にその結合どんなあなたはdependancyプロパティを使うことができます! 「propdp」と我々が持っていると思います。この場合には、二回タブをヒット:

public ObservableCollection<string> strs 
    { 
     get { return (ObservableCollection<string>)GetValue(strsProperty); } 
     set { SetValue(strsProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for strs. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty strsProperty = 
     DependencyProperty.Register("strs", typeof(ObservableCollection<string>), typeof(Container), new PropertyMetadata("")); 
をVisual Studioで

は、それが形に埋めることがたくさんがあるとして、このためにスニペットで構築を使用するのが最善です

答えて

1

これは私のために働いた。

XAML:

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

     <DataGrid ItemsSource="{Binding thing.stuff}" AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn Width="Auto" Header="String Contents" > 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding}" /> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
      </DataGrid.Columns> 
     </DataGrid> 

    </Grid> 
</Window> 

と背後にあるコード(C#の):

using System.Windows; 
using System.Collections.ObjectModel; 

namespace WpfApplication2 
{ 
    public class Thing 
    { 
     // make sure this is a property, not a field. 
     // furthermore, make sure it is public. 
     public ObservableCollection<string> stuff 
     { 
      get; set; 
     } 

     public Thing() 
     { 
      stuff = new ObservableCollection<string>(); 
      stuff.Add("A String"); 
      stuff.Add("Another String"); 
      stuff.Add("Yet Another String"); 
     } 
    } 

    public partial class MainWindow : Window 
    { 
     public Thing thing{get;set;} 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = this; 
      thing = new Thing(); 
     } 
    } 
} 

私はあなたの質問もう少し肉付けをお勧めします。 StackOverflowの目標は、質問は他のユーザーにとっても自分自身にとっても役立つことです。

EDIT:Terser XAML

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

     <DataGrid ItemsSource="{Binding thing.stuff}" AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Width="Auto" Binding="{Binding}" Header="String Contents" /> 
      </DataGrid.Columns> 
     </DataGrid> 

    </Grid> 
</Window> 
関連する問題