0
votes

I have DataGrid inside of the UserControl. Datagrid is bound to the ItemsSource with 50 items (50 rows). UserControl Width and Height are set to auto. How can I enable ScrollBar without specifying Height and Width of DataGrid? Maybe I miss something, because at this time designer shows me that my DataGrid Height is 5000 (based on Auto calculation), that's why ScrollBar is not showing. I want my DataGrid to fit Main Window container - Grid Row with star size. I don't want to use fixed sizes, because I want to use this view on different screen resolutions.

<UserControl x:Class="Treasury7MD.Views.Form7MDTableHeadeView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" >
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

        <Grid Grid.Row="1" Style="{StaticResource FormGridBorderStyle}" Margin="20,0,0,20" >
            <Grid.Resources>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="BorderBrush" Value="Black"/>
                </Style>
            </Grid.Resources>


                <DataGrid x:Name="dg" ItemsSource="{Binding KEKVs}" CanUserAddRows="False" AutoGenerateColumns="False" RowHeaderWidth="0" SelectionUnit="Cell" HorizontalContentAlignment="Stretch" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
                <DataGrid.Columns>
                <DataGridTemplateColumn Header="1">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Width="{Binding ActualWidth, ElementName=IndicatorTbl}" Text="{Binding Indicator, Mode=TwoWay}" TextWrapping="Wrap"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="2">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Width="{Binding ActualWidth, ElementName=KEKVTbl}" Text="{Binding Name, Mode=TwoWay}" TextWrapping="WrapWithOverflow"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="3">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Width="{Binding ActualWidth, ElementName=RowCodeTbl}" Text="{Binding RowCode, Mode=TwoWay}" TextWrapping="WrapWithOverflow"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="4">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <customControlls:NumericTextBox Style="{StaticResource NumericTextboxStyle}" Width="{Binding ActualWidth, ElementName=AccRecAtTheBeginingTbl}" Text="{Binding AccountsReceivable.AtTheBeginingOfTheYear, UpdateSourceTrigger=LostFocus}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="5">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <customControlls:NumericTextBox Style="{StaticResource NumericTextboxStyle}" Width="{Binding ActualWidth, ElementName=AccRecAtTheEndTbl}" Text="{Binding AccountsReceivable.AtTheEndOfTheReportingPeriod, Mode=TwoWay}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="6">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <customControlls:NumericTextBox Style="{StaticResource NumericTextboxStyle}" Width="{Binding ActualWidth, ElementName=AccRecOverdueTbl}" Text="{Binding AccountsReceivable.OverdueAtTheEndOfTheReportingPeriod, Mode=TwoWay}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="6">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <customControlls:NumericTextBox Style="{StaticResource NumericTextboxStyle}" Width="{Binding ActualWidth, ElementName=AccRecWrittenOffTbl}" Text="{Binding AccountsReceivable.WrittenOffSinceTheBeginningOfTheYear, Mode=TwoWay}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
    </Grid>
</Grid>

This is View in which UserControl is located

<Window x:Class="Treasury7MD.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MainWindow" WindowState="Maximized">
<Grid>

    <Grid Name="g" >
        <Grid.RowDefinitions>
            <RowDefinition MaxHeight="20"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Name="x" Height="*" MinHeight="100"></RowDefinition>

        </Grid.RowDefinitions>
        <Button Content="Collapse" Click="Button_Click"></Button>
        <views:OrganizationInfoView x:Name="z" Grid.Row="1"></views:OrganizationInfoView>
        <ScrollViewer Grid.Row="2" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" >

                <views:Form7MDTableHeadeView Grid.Row="2" ></views:Form7MDTableHeadeView>

        </ScrollViewer>
    </Grid>
   </Grid>

1
As easy solution, you can put datagrid inside scroll viewer - tym32167
This sounds like a layout issue higher up in your DOM. When you say you have the DataGrid in a Grid of * height, is that parent Grid by chance the child of a StackPanel? You shouldn't need to define fixed sizes but it sounds like your layout isn't providing the boundaries necessary to invoke the embedded scrollviewer built into the DataGrid template. - Chris W.
ScrollViewer gives the same result. - Mrg Gek
Chris W, no it's not the child of the StackPanek. - Mrg Gek
try give some MaxHeight to Datagrid - Ayyappan Subramanian

1 Answers

0
votes

Removed ScrollViewer wrapped around UseControl and DataGrid's ScrollBar worked as intended.