I have ScrollViewer where is displayed page content. Inside I have a DataGrid, but when I put DataGrid inside ScrollViewer width of columns is lost. So I readed here http://stackoverflow.com/questions/17875765/wpf-scrollviewer-around-datagrid-affects-column-width
that I need to bind width of parent to my DataGrid and it's ok but what's the problem.
When width of window is increase width of DataGrid is increased too but when width of window is decreased width of DataGrid doesn't change. What I would like to have is when width of is change width of DataGrid is changed too.
This is sample XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<!--Page Content-->
<Grid x:Name="grid">
<DataGrid Width="{Binding ElementName=grid, Path=ActualWidth}">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Header 1" />
<DataGridTextColumn Width="*" Header="Header 2" />
<DataGridTextColumn Width="*" Header="Header 3" />
<DataGridTextColumn Width="*" Header="Header 4" />
<DataGridTextColumn Width="*" Header="Header 5" />
<DataGridTextColumn Width="*" Header="Header 6" />
<DataGridTextColumn Width="*" Header="Header 7" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</ScrollViewer>
</Window>
EDITED: Now my MainWindow looks like:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:my="clr-namespace:WpfApplication1"
x:Name="window1">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<my:MyGrid />
</ScrollViewer>
</Window>
and my control:
<UserControl x:Class="WpfApplication1.MyGrid"
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"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<DataGrid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=ActualWidth}">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Header 1" />
<DataGridTextColumn Width="*" Header="Header 2" />
<DataGridTextColumn Width="*" Header="Header 3" />
<DataGridTextColumn Width="*" Header="Header 4" />
<DataGridTextColumn Width="*" Header="Header 5" />
<DataGridTextColumn Width="*" Header="Header 6" />
<DataGridTextColumn Width="*" Header="Header 7" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>