1
votes

I know this question has been answered before but looking at most of them, they use code behind and I am under the understanding that is never ok in MVVM, also others use MVVM pattern. I refuse to use a pattern as I want to learn from the ground up. I am able to bind the ItemsSource correctly but how do I bind to the datagrid headers and such.

I would think this is common but what I want is to be able to have a "table" of data with headers. I want the user to be able to right click in the header row and toggle columns off and on. That part I am not as worried about figuring out how to do.

I am asking how to have a datagrid with dynamic columns/headers without codebehind or a framework. I am not sure how to bind from the viewmodel to a datagrid.

2
WPF is a framework. So is .NET. Don't get caught up in "correctness;" use best practices, but temper them with some common sense. - Robert Harvey
I understand, I just don't want to fall into the trap of I don't know how to do this so I will just use a helper framework to do it for me. I was meaning frameworks like MVVM light and such. - AnthonyFG
"I am under the understanding that is never ok in MVVM", that is plain wrong. A correct statements would be that "it is rarely necessary". - H.B.
HB is correct that it is rarely necessary to use code-behind in MVVM, but keep in mind that code-behind is absolutely OK if the code-behind relates to your View only. You typically do not ever need to reference your ViewModel or Models in the code-behind, but every once in a while it is necessary to. - Rachel

2 Answers

0
votes
 <DataGrid AutoGenerateColumns="False" Height="256" HorizontalAlignment="Left" Name="dgEmp" VerticalAlignment="Top" Width="490"  ItemsSource="{Binding DeleteData,Mode=TwoWay}" Margin="6,7,0,0" Grid.RowSpan="3">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="ID" Binding="{Binding ID,Mode=TwoWay}" IsReadOnly="True" Visibility="Hidden"/>
                        <DataGridTextColumn Header="Description" Binding="{Binding Description,Mode=TwoWay}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Amount" Binding="{Binding Amount,Mode=TwoWay}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Date" Binding="{Binding Date,Mode=TwoWay}" IsReadOnly="True"/>
                        <DataGridTextColumn Header="Remark" Binding="{Binding Remark,Mode=TwoWay}" IsReadOnly="True"/>
                        <DataGridTemplateColumn>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="Update" x:Name="btnUpdate"
                            Click="btnUpdate_Click"></Button>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

In this Example ItemSource of DataGrid is DeleteData which is one ObservableCollection<Expense_Submit>

Expense_Submit is one class in that i have defined all properties .it's look like

public class Expense_Submit
{


 private int _ID;

    private string _UserID;

    private string _Description;

    private string _Amount;

    private DateTime _Date;

    private string _Remark;


  }
0
votes

For That You have to bind Visibility Property.

Visibility={binding IDVisibile,mode=TwoWay}

in your ViewModel Create one property

 private Visibility _IDVisibile;

            public Visibility IDVisibile
            {
                get { return _BorderVisibility; }
                set { _BorderVisibility = value; RaisePropertyChanged("IDVisibile"); }
            }