2011-05-10 63 views
1

我需要能夠避免獲得負值。當窗口瀏覽器調整大小時,我需要datagrid縮小,但不會達到負值以避免出現錯誤。那可能嗎?先謝謝你。如何避免獲得負值?

這裏是我的代碼:

public SilverlightResizeTest() 
    { 
     InitializeComponent(); 
     // Set the height for the DataGrid when the browser window changes size 
     App.Current.Host.Content.Resized += new System.EventHandler(Content_Resized); 

     // Set the initial height for the DataGrid 
     double x = App.Current.Host.Content.ActualHeight; 
     if (x != 0) 
     { 
      DataGrid.Height = (x - 485.0); 
     } 
    } 

    void Content_Resized(object sender, System.EventArgs e) 
    { 
     // Set the height for the DataGrid when the browser window changes size 
     double x = App.Current.Host.Content.ActualHeight; 
     if (x != 0) 
     { 
      DataGrid.Height = (x - 485.0); 
     } 
    } 

回答

3
DataGrid.Height = Math.Max(0.0, x - 485.0); 
+0

它的偉大工程。感謝您的幫助!我高度評價它。 – vladc77 2011-05-10 16:50:26

3

無論這個

DataGrid.Height = Math.Max(0, x - 485.0); 

或本

DataGrid.Height = (x - 485.0); 
if(DataGrid.Height < 0) DataGrid.Height = 0; 

或...

真的,有很多方法可以做到這一點。

1

你可以試試這個,ONN Content_Resized方法

DataGrid.Height = x > (485.0) ? (x - 485.0) : DataGrid.Height; 

這樣,如果新的高度將是0,它反而會保持以前的高度,也可以定義一個最小高度Four的數據網格:和使用這行代替:

DataGrid.Height = x > (485.0 + minHeight) ? (x - 485.0) : DataGrid.Height; 
1

爲什麼不檢查它? :

DataGrid.Height = Math.Max(0 , x - 485.0); 
1
DataGrid.Height = Math.Max((x - 485.0), 0.0); 
+0

它很好用。實際上,所有建議的實現方法都適用於我。謝謝大家的幫助!我高度評價它。 – vladc77 2011-05-10 16:49:53