2017-02-25 30 views
0

我正在創建多行和多列的數據網格。我已經成功創建了XAML中的數據網格,並在代碼中創建了列,但發現我有一些限制。我需要更好地理解綁定,並希望將我的代碼轉換爲使用綁定在XAML中創建數據網格。列正在被複制。這裏是我以前的代碼:之前使用XAML創建DataGrid並在WPF中進行綁定

XAML:

<DataGrid Name="dtGrid" Loaded="GridLoaded" VirtualizingPanel.IsVirtualizing="False" Height="365" Width="558" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="54,74,0,0" BorderThickness="1" BorderBrush="Black"> 
     <DataGrid.Resources> 
      <Style TargetType="{x:Type DataGridCell}"> 
       <Style.Triggers> 
        <Trigger Property="DataGridCell.IsSelected" Value="True"> 
         <Setter Property="Background" Value="#FF9DF3D6" /> 
         <Setter Property="Foreground" Value="#000000" /> 
        </Trigger> 
       </Style.Triggers> 
       <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" /> 
       <EventSetter Event="LostFocus" Handler="DataGridCell_OnCellLostFocus" /> 
      </Style> 
     </DataGrid.Resources> 
    </DataGrid> 

XAML變化:

<DataGrid HeadersVisibility="Column" Name="dtGrid" Loaded="GridLoaded" VirtualizingPanel.IsVirtualizing="False" Height="365" Width="558" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="54,74,0,0" BorderThickness="1" BorderBrush="Black"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Field" Binding="{Binding Field}" /> 
      <DataGridTextColumn Header="Size" Binding="{Binding Size, Mode=TwoWay}" /> 
      <DataGridCheckBoxColumn Header="Right Justify" Binding="{Binding RightJustify, Mode=TwoWay}" /> 
      <DataGridCheckBoxColumn Header="Left Justify" Binding="{Binding LeftJustify, Mode=TwoWay}" /> 
      <DataGridCheckBoxColumn Header="Left Zero Fill" Binding="{Binding LeftZeroFill, Mode=TwoWay}" /> 
      <DataGridCheckBoxColumn Header="Right Zero Fill" Binding="{Binding RightZeroFill, Mode=TwoWay}" /> 
     </DataGrid.Columns> 
     <DataGrid.Resources> 
      <Style TargetType="{x:Type DataGridCell}"> 
       <Style.Triggers> 
        <Trigger Property="DataGridCell.IsSelected" Value="True"> 
         <Setter Property="Background" Value="#FF9DF3D6" /> 
         <Setter Property="Foreground" Value="#000000" /> 
        </Trigger> 
       </Style.Triggers> 
       <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" /> 
       <EventSetter Event="LostFocus" Handler="DataGridCell_OnCellLostFocus" /> 
      </Style> 
     </DataGrid.Resources> 
    </DataGrid> 

C#:DataGrid的

private void DisplayFieldLengths(string strFLFileName, string[,] strFieldInfo) 
    { 
     int intDisplayCnt = 0; 
     string strData, strFieldSize = ""; 
     bool blnRightJustify, blnLeftJustify, blnLeftZeroFill, blnRightZeroFill; 


     blnRightJustify = false; 
     blnLeftJustify = false; 
     blnLeftZeroFill = false; 
     blnRightZeroFill = false; 
     intTotalRowSize = 0; 
     lblFLInfo.Content = "File: " + strFLFileName; 
     DataTable dtGridData = new DataTable(); 
     dtGridData.Columns.Add("Field", typeof(string)); 
     dtGridData.Columns.Add("Size", typeof(string)); 
     dtGridData.Columns.Add("RightJustify", typeof(bool)); 
     dtGridData.Columns.Add("LeftJustify", typeof(bool)); 
     dtGridData.Columns.Add("LeftZeroFill", typeof(bool)); 
     dtGridData.Columns.Add("RightZeroFill", typeof(bool)); 

     try 
     { 
      int intArraySize = strFieldInfo.GetLength(0); 

      for (intDisplayCnt = 0; intDisplayCnt < strFieldInfo.GetLength(0); intDisplayCnt++) 
      { 
       strFieldSize = strFieldInfo[intDisplayCnt, 1]; 
       Int32 intStringNumValue; 
       bool blnValueIsNumber = Int32.TryParse(strFieldSize, out intStringNumValue); 

       if (strFieldSize != null && !(string.IsNullOrEmpty(strFieldSize)) && blnValueIsNumber) 
       { 
        if (strFieldSize.Length == 1) 
        { 
         strFieldSize = "0" + strFieldSize; 
        } 

        if (string.IsNullOrEmpty(strFieldInfo[intDisplayCnt, 2])) 
        { 
         blnRightJustify = false; 
        } 
        else 
        { 
         blnRightJustify = Convert.ToBoolean(strFieldInfo[intDisplayCnt, 2]); 
        } 

        if (string.IsNullOrEmpty(strFieldInfo[intDisplayCnt, 3])) 
        { 
         blnLeftJustify = false; 
        } 
        else 
        { 
         blnLeftJustify = Convert.ToBoolean(strFieldInfo[intDisplayCnt, 3]); 
        } 

        if (string.IsNullOrEmpty(strFieldInfo[intDisplayCnt, 4])) 
        { 
         blnLeftZeroFill = false; 
        } 
        else 
        { 
         blnLeftZeroFill = Convert.ToBoolean(strFieldInfo[intDisplayCnt, 4]); 
        } 

        if (string.IsNullOrEmpty(strFieldInfo[intDisplayCnt, 5])) 
        { 
         blnRightZeroFill = false; 
        } 
        else 
        { 
         blnRightZeroFill = Convert.ToBoolean(strFieldInfo[intDisplayCnt, 5]); 
        } 


        strData = strFieldInfo[intDisplayCnt, 0] + "|" + strFieldSize + "|" + blnRightJustify + "|" 
         + blnLeftJustify + "|" + blnLeftZeroFill + "|" + blnRightZeroFill; 
        dtGridData.Rows.Add(strData.Split('|')); 
       } 
       else 
       { 
        strFieldSize = "01"; 
       } 
      } 

      dtGrid.ItemsSource = dtGridData.DefaultView; 
      dtGrid.AutoGeneratingColumn += dtGrid_AutoGeneratingColumn; 
      intTotalRowSize = GetFLTotalFileSize(strFieldInfo); 
      lblRowSize.Content = " Total row length: " + intTotalRowSize; 
      intTotalNumberColumns = strFieldInfo.GetLength(0); 
      lblNumFields.Content = "Number of fields: " + intTotalNumberColumns; 
     } 
     catch (Exception e) 
     { 
      string strMsg; 

      strMsg = "FixedLengths->DisplayFieldLengths, error '" + e.Message + "' has occurred."; 
      System.Windows.MessageBox.Show(strMsg); 
     } 
    } 

回答

0

AutoGenerateColumns屬性默認爲真。因此,在代碼中使用dtGrid.ItemsSource = dtGridData.DefaultView;時,它將根據DataTable的列生成列。

但是,您要添加的列手動過,在XAML中這些行:

<DataGrid.Columns> 
    <DataGridTextColumn Header="Field" Binding="{Binding Field}" /> 
     .... 
</DataGrid.Columns> 

只是刪除它們,一切都會好起來。

當然,您也可以設置AutoGenerateColumns="false",並且您將再次看到DataTable中每列的只有一列。

+0

#Ron - 現在我覺得很蠢。我以爲我的代碼中有AutoGenerateColumns =「false」。謝謝! – Cass