开发者

Non-Working WPF Binding

I am a WPF noob and i hope to stop being so one day (Lol)

Anyways, i am having issues binding a DataTable (Obtained from an OleDBDataAdapter) to a DataGrid. Even though the DataTable is NOT empty, i get nothing on the DataGrid, it only works if i manually setup the ItemsSource property in code-behind. Help? [I want to make this automatic so any changes made to the grid are reflected into the DataTable, enabling me to just use OleDBCommandbuilder to update the Access database itself (Yes, i am lazy :)). I was thinking of extending DataTable to implement INotifyCollectionChanged but im not sure if that's do-able :S]

XAML:

            <Grid>
                <DataGrid Grid.Column="0" Grid.ColumnSpan="2" x:Name="ListaArticulos" 
                          Padding=开发者_JS百科"-5,0,0,0" AutoGenerateColumns="False" ItemsSource="{Binding Articulos}">
                    <DataGrid.Columns>
                        <DataGridTextColumn IsReadOnly="True" Width="Auto" Header="ID" Binding="{Binding ID}"/>
                        <DataGridTextColumn Header="Nombre Articulo" Binding="{Binding Nombre}"/>
                        <DataGridTextColumn Header="Categoria" Binding="{Binding Categoria}"/>
                        <DataGridTextColumn Header="Valor" Binding="{Binding Valor}"/>
                        <DataGridTextColumn Header="Cantidad" Binding="{Binding Cantidad}" />
                        <DataGridTextColumn Header="Color" Binding="{Binding Color}" />
                        <DataGridTextColumn Header="Material" Binding="{Binding Material}" />
                        <DataGridTextColumn Header="Referencia" Binding="{Binding Referencia}" Width="*"/>
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>

Code-Behind:

    public DataTable _Articulos = new DataTable();

    public DataTable Articulos { get { return _Articulos; } }

    public MainWindow()
    {
        InitializeComponent();
        ListaArticulos.DataContext = this;
        ReadDB();
    }

ReadDB() updates the Articulos DB.

Thanks in advance.


your code

    InitializeComponent();
    ListaArticulos.DataContext = this;
    ReadDB();

change to

    ReadDB();
    InitializeComponent();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜