Avalan
Avalan

Reputation: 159

How do I have DataGrid Templates and change them as needed

I am busy with a basic function where the user is presented with a ListBox that contains the tables within a database. To the right of the ListBox is a DataGrid which displays the fields in the table. This means that the columns will change every time another table is selected and is where my problem lies. I am aware of the auto column generation feature but since I would like to provide my own column names I need to do it manually. I have read about the event that you can handle to change the names when auto column generation is active but since I am using MVVM I would like to steer clear of using the code behind file for event handling. The idea I have is to rather create a couple of templates and then use a trigger inside the DataGrid to select the correct column layout. This is what I have at the moment. The templates:

<UserControl.Resources>
    <DataTemplate x:Key="template_measurement">
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding id}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding name}"/>
                <DataGridTextColumn Header="Latitude" Binding="{Binding source_latitude}"/>
                <DataGridTextColumn Header="Longitude" Binding="{Binding source_longitude}"/>
            </DataGrid.Columns>
        </DataGrid>
    </DataTemplate>

    <DataTemplate x:Key="template_realestate">
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding id}"/>
                <DataGridTextColumn Header="Name" Binding="{Binding name}"/>
            </DataGrid.Columns>
        </DataGrid>
    </DataTemplate>
</UserControl.Resources>  

The DataGrid:

<DataGrid AutoGenerateColumns="False" 
          Name="grdData" 
          HorizontalAlignment="Left" 
          Margin="10,10,0,0" 
          Grid.Row="2" 
          VerticalAlignment="Top" 
          Height="200" 
          Width="500"
          ItemsSource="{Binding Path=SelectedTable}">

        <DataGrid.Triggers>
            <Trigger Property="{Binding Path=SelectedTableName}" Value="measurement">
                <Setter Property="DataGrid" Value="{StaticResource template_measurement}"/>
            </Trigger>
        </DataGrid.Triggers>

</DataGrid>

The above doesn't work and I suspect that it might be seriously wrong but hopefully shows what I am attempting to achieve. The SelectedTableName property is a string that holds the name of the currently selected table.

How do I get the DataGrid to use one of the templates that I have provided based on the SelectedTableName value?

Upvotes: 0

Views: 157

Answers (1)

Muds
Muds

Reputation: 4116

Well I think half of your approach is correct half is not,

instead of havind data templates and then set it to grid you can have ContentControl and then set its content template like you are trying right now.

Upvotes: 1

Related Questions