Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 472.2k

How to display the columns names in the grid using wpf ui?

Jun 5 2024 12:31 PM

Hi Team

I have a grid, but the columns names are not displaying and dont know from my code what am missing.

 



    <UserControl.Resources>
        <CollectionViewSource x:Key="StockCode" Source="{Binding Code}"/>
        <CollectionViewSource x:Key="StockDescription" Source="{Binding Description}"/>
    </UserControl.Resources>

    <Grid Background="White" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,29,0,10" Width="1766">
        <Label Content="Premix Batch Material Management" BorderThickness="1.5" BorderBrush="#FF0B0B0B" Background="#FF383838" Foreground="White" FontSize="18" HorizontalContentAlignment="Center" VerticalAlignment="Top" Margin="10,10,0,0" FontWeight="SemiBold" />
        <Rectangle HorizontalAlignment="Left" Height="351" Stroke="#FFCFCFD1" VerticalAlignment="Top" Width="260" Margin="14,104,0,0"/>
        <Label Content="Recipe Details" FontWeight="SemiBold" HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalAlignment="Top" Width="260" BorderBrush="#FFBEBEBE" Background="#FFE3E3E3" BorderThickness="1.5" Foreground="Black" Height="30" VerticalContentAlignment="Center" FontSize="12" Margin="14,103,0,0"/>

        <Label x:Name="lblActive_Copy2" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Description:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,140,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtDescription" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,137,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy3" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Batch Size:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,168,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtBatchSize" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,165,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>

        <Label x:Name="lblDriverName_Copy" FontWeight="SemiBold" Visibility="Visible" Content="Recipe Code:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="179" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,65,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <ComboBox x:Name="cbxRecipe" TabIndex="1" IsTextSearchEnabled="True" IsEditable="True" IsTextSearchCaseSensitive="False" Foreground="Black" HorizontalAlignment="Left" Margin="207,63,0,0" VerticalAlignment="Top" Width="214" SelectionChanged="cbxRecipe_SelectionChanged">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel />
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
        </ComboBox>

        <DataGrid ItemsSource="{Binding Macro}" HorizontalAlignment="Left" Margin="17,300,0,0" VerticalAlignment="Top" Height="150" Width="500">
            <!-- IDX Column -->
            <DataGridTextColumn Binding="{Binding idx}" Header="IDX" Width="Auto" IsReadOnly="True" FontWeight="SemiBold" />

            <!-- Stock Code Column -->
            <DataGridTemplateColumn Header="Stock Code">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding [Stock Code]}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox Height="22" Width="Auto" ItemsSource="{Binding Source={StaticResource StockCode}}" DisplayMemberPath="Code" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

            <!-- Stock Description Column -->
            <DataGridTemplateColumn Header="Stock Description">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding [Stock Description]}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox Height="22" Width="Auto" ItemsSource="{Binding Source={StaticResource StockDescription}}" DisplayMemberPath="Description" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

            <!-- Percentage Column -->
            <DataGridTextColumn Binding="{Binding Percentage}" Header="Percentage" Width="Auto" FontWeight="SemiBold">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="TextAlignment" Value="Center" />
                        <Setter Property="VerticalAlignment" Value="Center" />
                        <Style.Triggers>
                            <Trigger Property="Text" Value="Yes">
                                <Setter Property="Background" Value="LightGreen" />
                            </Trigger>
                            <Trigger Property="Text" Value="No">
                                <Setter Property="Background" Value="#FFFF6969" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

            <!-- Kilograms Column -->
            <DataGridTextColumn Binding="{Binding Kilograms}" Header="Kilograms" Width="Auto" FontWeight="SemiBold" IsReadOnly="True">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="TextAlignment" Value="Center" />
                        <Setter Property="VerticalAlignment" Value="Center" />
                        <Style.Triggers>
                            <Trigger Property="Text" Value="Yes">
                                <Setter Property="Background" Value="LightGreen" />
                            </Trigger>
                            <Trigger Property="Text" Value="No">
                                <Setter Property="Background" Value="#FFFF6969" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid>
    </Grid>

 


Answers (2)