2012-05-04 73 views
0

我的問題是關於頁面表單中的DataGrid,使用C#.NET 4.0進行編程。該應用程序用於桌面,而不是Web或Silverlight。設置顏色選擇DataGrid的整行,並在更改背景顏色後的MouseOver行爲

我改變了我們頁面的DataGrid,而不是改變背景。不幸的是,當我選擇一行時,它只會變成藍色(選擇標識的顏色)隻影響該行的列。在這些數據網格中,我留下了一些空間。我需要做的是完全選擇該行,包括該空白。

另一件改變的事情是鼠標在任何記錄上的鼠標行爲。在這個變化之後,現在這種行爲不再發生了。

任何線索我需要做什麼?

編輯:添加代碼:

我的轉換器:

public class RetornaCorFundoGrid : DependencyObject, IValueConverter 
{ 
    public static DependencyProperty CorFundoGridParameterProperty = 
     DependencyProperty.Register("CorFundoGridParameter", typeof(IEnumerable<Object>), typeof(RetornaCorFundoGrid)); 

    public IEnumerable<Object> CorFundoGridParameter 
    { 
     get { return ((IEnumerable<Object>)GetValue(CorFundoGridParameterProperty)); } 
     set { SetValue(CorFundoGridParameterProperty, value); } 
    } 

    public object Convert(Object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     try 
     { 
      if (System.Convert.ToInt16(value) < 5) 
       return Brushes.BlueViolet; 
      if (System.Convert.ToInt16(value) < 15) 
       return Brushes.CadetBlue; 
      else 
       return Brushes.Coral; 
     } 
     catch (Exception) 
     { 
      return Brushes.Black; 
     } 

    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

我綁定反射:

<ut:BindingReflector Target="{Binding Mode=OneWayToSource, Source = {StaticResource RetornaCorFundoGrid}, Path=CorFundoGridParameter}" 
         Source="{Binding Parameters, Mode=OneWay}" /> 

我行樣式:

<DataGrid.RowStyle> 
    <Style TargetType="{x:Type DataGridRow}"> 
     <Setter Property="Background" Value="{Binding Path=Id, Converter={StaticResource RetornaCorFundoGrid}}"/> 
    </Style> 
</DataGrid.RowStyle> 

回答

0

我發現我的問題...我們使用ResourceDictionary.xaml。在它裏面,我們有一個DataGridRow的行爲定義。當我在此組件中定義它時,我會覆蓋它。

因此,要解決這個問題,香港專業教育學院增加了一個支持算法FMP:

<DataGrid.RowStyle> 
      <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}"> 
       <Setter Property="Background" Value="{Binding Path=Id, Converter={StaticResource RetornaCorFundoGrid}}"/> 
      </Style> 
     </DataGrid.RowStyle > 

簡單的,但惱人的..

感謝您的幫助!

0

我在理解您要求的內容時遇到了一些麻煩,但也許這會有所幫助。爲了製作一個只能按全行選擇的表格,可以將SelectionMode設置爲FullRowSelect。然後,無論您何時點擊桌面上的任何位置,您都會看到您單擊的單元格的整行。

至於着色單元,可以通過列當前行中進行迭代,並同時設置背景色和的SelectionBackColor任何你想要的顏色,就像這樣:

foreach (DataGridCell cell in myRow.Cells) 
{ 
    cell.Style.BackColor = myColor; 
    cell.Style.SelectionBackColor = myColor; 
} 

不知道什麼用鼠標回事過度行爲。

+0

不,不,不,我可以改變我的背景顏色。我的問題是,當我這樣做,並選擇一行,而不是整行改變選擇的顏色,因爲我的網格不填滿列。由於還有更多2或3列的空間,我希望使這個選擇顏色完全填充行柵格,即使沒有更多列填充。 可以澄清我的問題嗎? –

0

要爲DataGrid的整行着色,您需要爲DataGrid.RowStyle設置一個樣式,該樣式定義了該行的背景顏色。您可能需要將單元格的背景設置爲透明(以免隱藏行顏色)或更改它們以匹配行顏色。此外,您需要設置選擇顏色和處理鼠標懸停事件的EventTrigger。

+0

我已經爲我的問題添加了一些代碼。我已經爲rowStyle設置了一個樣式。我需要填寫沒有數據或列的行,但是網格上的剩餘空間。 E voltar o comportamento做鼠標懸停。 更好地闡明:我們已經創建了一個從DataGrid繼承的組件。我們在這個組件中定義MouseOver。這就是我們的問題,因爲它會自動帶來這種MouseOver效果,只有在我添加了這種顏色轉換之後,我們的MouseOver和整個行顏色選擇纔出現問題。 –