2010-10-21 75 views
2

this is my simple try-it application創建一個2行的網格。第一行的高度綁定到一個屬性。我分配給它的值只在運行時起作用。我試圖讓它在設計時也能正常工作,但我沒有這麼做(我用this thread來編寫我的應用程序)。初始網格行高不起作用

請幫我看看我想念的。謝謝!

[編輯]

爲什麼我這樣做是我想動態設置頂格行的高度,即原因。 Grid.Row="0",作爲標題欄的高度。在我的應用程序某處,視圖加載並重疊標題欄。

回答

2

你正試圖做一個非常奇怪的詭計,這不應該工作。嘗試進行以下更改。

MainWindow.xaml.cs - 儘量讓你的代碼保持清晰。

namespace WpfTryIt 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

MainWindow.xaml

<Window x:Class="WpfTryIt.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" 

     xmlns:s ="clr-namespace:WpfTryIt" 
     > 
    <Window.DataContext> 
     <s:FakeDataContext></s:FakeDataContext> 
    </Window.DataContext> 


     <Button Content="{Binding Path=BindingHeight}"/> 

</Window> 

和一個新的獨立的數據上下文類,根據不同的模式,它表現出不同:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 
using System.Windows; 

namespace WpfTryIt 
{ 
    public class FakeDataContext 
    { 
     public int BindingHeight 
     { 
      get 
      { 
       // Check for design mode. 
       if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)) 
       { 
        //in Design mode 
        return 100; 
       } 
       else 
       { 
        return 200; 
       } 
      } 
     } 
    } 
} 
+0

萬一FakeDataContext的構造需要一些論據,我怎樣才能修改你的代碼,使其工作? – 2010-10-22 10:54:42

+0

我也更新了我的問題,解釋爲什麼我嘗試'奇怪的技巧'。 – 2010-10-22 11:30:50

+0

@Nam Gi VU:在XAML中,只能創建具有空構造函數的對象。如果你仍然想用參數實例化一個對象,看看這個問題http://stackoverflow.com/questions/2335900/using-xamlreader-for-controls-that-does-not-have-a-default-constructor – 2010-10-22 12:10:41