0
我有我的WPF窗體中有多個列的datagrid。當打開表單並將列添加到數據網格時,表單會自動調整爲屏幕寬度。禁用DataGrid調整列添加
現在我知道發生這種情況是因爲我已將datagrid對齊設置爲拉伸,並將列寬設置爲*在我的代碼中。但是這些都需要設置,因爲我想要datagrid改變它的大小,當用戶調整窗口大小時(而不是當窗口打開時)填充datagrid寬度的列。
形式本身是在這裏:
<UserControl
/*...*/
d:DesignHeight="590" d:DesignWidth="896">
<Grid x:Name="Form" Loaded="Form_Loaded" MinWidth="896" MinHeight="590">
<Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="10,8,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.479,0.577" Height="27"/>
<ComboBox x:Name="combo" HorizontalAlignment="Left" Margin="68,10,0,0" VerticalAlignment="Top" Width="200" SelectionChanged="combo_SelectionChanged" Height="23"/>
<Button x:Name="buttonOne" Content="Button" HorizontalAlignment="Right" Margin="0,10,10,0" VerticalAlignment="Top" Width="75" Height="23"/>
<TextBox x:Name="box" HorizontalAlignment="Right" Height="23" Margin="0,10,95,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="200"/>
<DataGrid x:Name="view" HorizontalAlignment="Stretch" Margin="10,43" VerticalAlignment="Stretch" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" CanUserReorderColumns="False" SelectionMode="Single" IsReadOnly="True" ItemsSource="{Binding Path=List}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="*" Binding="{Binding Path=Name}" Visibility="Visible"/>
/*...*/
</DataGrid.Columns>
</DataGrid>
<Button x:Name="buttonTwo" Content="Button" HorizontalAlignment="Right" Margin="0,0,10,10" DockPanel.Dock="Right" Width="75" Height="23" Click="buttonTwo_Click" VerticalAlignment="Bottom"/>
</Grid>
</UserControl>
而且它的創作是在這裏:
System.Windows.Window window = new System.Windows.Window();
Form form = new Form();
window.Content = form;
window.ResizeMode = System.Windows.ResizeMode.CanResizeWithGrip;
window.MinWidth = 916;
window.MinHeight = 633;
window.Title = "Form";
window.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
Uri iconUri = new Uri(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Image\\image.ico", UriKind.RelativeOrAbsolute);
window.Icon = BitmapFrame.Create(iconUri);
window.ShowDialog();
我試圖把數據網格具有固定的寬度和高度獨立的面板內。添加列後,我也改變了窗口大小,但這些解決方案都沒有按計劃工作。
當窗口已打開並添加列時,如何避免調整數據網格和窗口的大小(以便稍後調整大小)?
你說你自己DataGrid得到調整大小,因爲你將它設置爲伸展。你想要發生什麼? –
我想要datagrid不拉伸,當列添加。但是當用戶調整窗口本身的大小時,我希望它能夠與列一起展開。當窗戶打開時,窗戶的寬度應小於整個窗戶的寬度。 – danrodi