2015-10-24 149 views
1

我有WPF數據網格的網格列在這裏添加的是XAMLWPF DataGrid列寬度調整時最大限度地

<DataGrid x:Name="grdProgramList" IsReadOnly="True" 
      Grid.Column="1" Grid.Row="1" 
      Width="{Binding Path=ActualWidth, ElementName=grid}" 
      RenderOptions.ClearTypeHint="Enabled" 
      TextOptions.TextFormattingMode="Display" 
      HeadersVisibility="All" 
      Margin="5" 
      SelectionMode="Single" 
      SelectionUnit="FullRow" 
      SelectionChanged="AzureDataGrid_Selected" 
      ItemsSource="{Binding Path=Uninstaller.ProgramCollection}" 
      SelectedItem="{Binding Uninstaller.SelectedProgramDetails, Mode=TwoWay}" 
      AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="PROGRAM NAME" 
          Binding="{Binding ProgramDetails.ProgramName}" 
          MinWidth="325" /> 
     <DataGridTextColumn Header="Publisher" 
          Binding="{Binding ProgramDetails.PublisherName}" /> 
     <DataGridTextColumn Header="Estimated Size (KB)" 
          Binding="{Binding ProgramDetails.EstimatedSize}" /> 
     <DataGridTextColumn Header="Version" 
          Binding="{Binding ProgramDetails.Version}" /> 
    </DataGrid.Columns> 
    <DataGrid.RowStyle> 
     <Style TargetType="{x:Type DataGridRow}"> 
      <EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/> 
      <Style.Triggers> 
       <!-- IsSelected --> 
       <MultiDataTrigger> 
        <MultiDataTrigger.Conditions> 
         <Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="true" /> 
        </MultiDataTrigger.Conditions> 
        <Setter Property="Background" Value="#CC119EDA" /> 
        <Setter Property="Foreground" Value="White" /> 
        <Setter Property="BorderBrush" Value="#CC119EDA" /> 
       </MultiDataTrigger> 
       <!-- IsHover --> 
       <MultiDataTrigger> 
        <MultiDataTrigger.Conditions> 
         <Condition Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true" /> 
        </MultiDataTrigger.Conditions> 
        <Setter Property="Background" Value="#66119EDA" /> 
        <Setter Property="BorderBrush" Value="#66119EDA" /> 
       </MultiDataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.RowStyle> 
</DataGrid> 

當它重新大小我想,以適應所有列的寬度網格寬度

現在看起來像這樣。

enter image description here

回答

1

應設置DataGridColumnWidth財產。它的類型爲DataGridLength,它允許您設置比例值。
您可以通過將該值設置爲number後跟*(例如2*,*默認爲1*)來完成此操作。
如果你希望每個column具有相同的width,將其設置爲*每一個:

<DataGrid.Columns> 
    <DataGridTextColumn Header="PROGRAM NAME" 
         Binding="{Binding ProgramDetails.ProgramName}" 
         MinWidth="325" Width="*" /> 
    <DataGridTextColumn Header="Publisher" 
         Binding="{Binding ProgramDetails.PublisherName}" Width="*" /> 
    <DataGridTextColumn Header="Estimated Size (KB)" 
         Binding="{Binding ProgramDetails.EstimatedSize}" Width="*"/> 
    <DataGridTextColumn Header="Version" 
         Binding="{Binding ProgramDetails.Version}" Width="*" /> 
</DataGrid.Columns> 

如果你只是想最後一列佔用的剩餘空間,只在一個設定的Width

<DataGrid.Columns> 
    <DataGridTextColumn Header="PROGRAM NAME" 
         Binding="{Binding ProgramDetails.ProgramName}" 
         MinWidth="325" /> 
    <DataGridTextColumn Header="Publisher" 
         Binding="{Binding ProgramDetails.PublisherName}" /> 
    <DataGridTextColumn Header="Estimated Size (KB)" 
         Binding="{Binding ProgramDetails.EstimatedSize}" /> 
    <DataGridTextColumn Header="Version" 
         Binding="{Binding ProgramDetails.Version}" Width="*" /> 
</DataGrid.Columns>