Datagrid WPF

Mar 27, 2013 at 11:08 AM
Hi all.

I'm more or less new to WPF which means the whole binding issue is new stuff to me.

Anyways, I am trying to bind a List<T> to a DataGrid

using the: nameOfDataGrid.ItemsSource = nameOfList;

Now here is my problem.. Filling the list with objects, and adding the list to the DataGrids ItemsSource is not a problem, however when I do this, the data will not be shown unless i'm in my XAML is adding to my column a Binding="{Binding}" This binding will show my list.ToString(), but will just fit it into the first column. What I need is to fill all my columns with my objects properties.

I've been reading up on this subject, and I often see people using; Binding="{Binding path=Variable}"

Now here is my problem.. My properties of my objects is located in a seperate file/class, and somehow it is not able to find it / reach it. (I think)

I will add a short code snippet of what i'm doing.

Code Behind:

public class myObject
{
private string string1; {get; set} <-- is included for each datafrield
private string string2; {get; set}
public myObject(string string1, string string2)
{
this.string1 = string1;
this.string2 = string2;
}

public override string ToString()
{
return string1; <--- i did this to just see if i could find the objects properties through codeBehind
}
}

public partial class SomeControl: UserControl
{
List<myObject> ListObject= new List<myObject>();

public UCSkaderegistrering(System.Xml.XmlNode XmlNode)
{
  InitializeComponent();
  myDataGrid.ItemsSource = ListDamage;
}

private void insertIntoDataGrid()
{

  myObject mO= new myObject(TextBlockString1.Text, TextBlockString2.Text);

  ListObject.Add(mO);
  myDataGrid.Items.Refresh();
}

private void ButtonCreateNew_Click(object sender, RoutedEventArgs e)
{
  insertIntoDataGrid();

}

XAML:

<DataGrid Name="myDataGrid" AutoGenerateColumns="False" CanUserResizeColumns="False" CanUserReorderColumns="False" IsReadOnly="True" VerticalScrollBarVisibility="Auto" Height="100" HorizontalAlignment="Left" Margin="0,5,0,5" VerticalAlignment="Top" Width="1080" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="String1" Binding="{Binding}" Width="80" />
                    <DataGridTextColumn Header="String2" Binding="{Binding}" Width="100"/>
                </DataGrid.Columns>
            </DataGrid>

I know the Binding="{Binding}" is wrong but it's the only thing right now that gives me a result.. The result comes from the Lists ToString() method...

Now.. what i want to know is, how can i point out my properties string1 and string2 in the path and get a result, because Binding="{Binding path=string1"} is not working for me.

All help is appreciated. Writing ItemsSource={Binding ListObject} in XAML under myDataGrid is not working either since I set it in my Code behind files.

Thanks in advance
Mar 27, 2013 at 12:30 PM
i dont think this is related to modern ui. i would ask at stackoverflow if i were you.
Mar 27, 2013 at 12:46 PM
Yes I thought I asked at the wrong place. Thank you for your reply though.