2011-10-05 110 views
1

有人請幫我解決這個問題。我有一個數據網格,我需要一切都具有完全可擴展的大小。我對列標題沒有任何問題,它們都可以正確縮放。我的問題是針對單個細胞,他們似乎不尊重它們的約束力。WPF如何綁定到私有變量

我不得不子類DataGridTextColumn添加一些自定義功能。我有一個名爲CreateDataGridColumn的方法,它返回對ExtendedDataGridTextColumn的引用。這些列然後被添加到數據網格。數據綁定本身工作正常,網格顯示所有正確的數據。網格高度的綁定似乎設置了初始值,但一旦顯示網格,如果它所綁定的變量發生更改,它不會更改高度。

下面是一些代碼:

 _customDataGrid.TitleAreaHeight = new GridLength(GlobalVariables.DesignerPreviewHeight * (_titleHeightPercentage/100)); 
     _customDataGrid.SetHeaderFontSize(GlobalVariables.DesignerPreviewHeight * (_headerHeightPercentage/100)); 
     _fontSize = GlobalVariables.DesignerPreviewHeight * (_gridTextHeightPercentage/100); 

前兩行做什麼,他們都應該更改:

private ExtendedDataGridTextColumn CreateDataGridColumn(EntityBase dataColumn, FormatConditionGroup formatConditionGroup) 
    { 
     ExtendedDataGridTextColumn newColumn = new ExtendedDataGridTextColumn(dataColumn); 
     DataTemplate dataTemplate = new DataTemplate(); 
     String textBlockName = "Text" + dataColumn.EntityId; 
     String columnTag = dataColumn.GetPropertyValue("Tag"); 

     // Create the TextBlock that will display the cell contents 
     FrameworkElementFactory textBlockFNFactory; 
     textBlockFNFactory = new FrameworkElementFactory(typeof(TextBlock)); 

     _gridTextHeightPercentage = dataColumn.GetPropertyDouble("GridFontSize", Constants.DefaultFontHeightPercent)/2.8; 
     _fontSize = GlobalVariables.DesignerPreviewHeight * (_gridTextHeightPercentage/100); 

     Binding binding = new Binding(); 
     binding.Source = _fontSize; 
     textBlockFNFactory.SetBinding(TextBlock.FontSizeProperty, binding); 

     // Do a whole bunch of stuff here 

     // Create a border so that the label background does not obscure the grid lines 
     FrameworkElementFactory borderFNFactory; 
     borderFNFactory = new FrameworkElementFactory(typeof(Border)); 
     borderFNFactory.AppendChild(textBlockFNFactory); 

     // Add type to data template 
     dataTemplate.VisualTree = borderFNFactory; 

     newColumn.CellTemplate = dataTemplate; 

     return newColumn; 
    } 

然後,我有以下方法上SizeChanged事件爲DataGrid解僱標題區域的高度,這是我添加到我的數據網格並更改標題高度。雖然_fontSize變量的更新不會更改數據網格單元格文本高度。

更新

按照建議我添加一個依賴屬性本身。

public static readonly DependencyProperty GridFontHeightProperty = DependencyProperty.Register("GridFontHeight", typeof(double), typeof(CustomDataGrid)); 

然後將我的綁定代碼更改爲此。

 binding = new Binding(); 
     binding.Path = new PropertyPath("GridFontHeight"); 
     textBlockFNFactory.SetBinding(TextBlock.FontSizeProperty, binding); 

然後在我的大小更改添加此。

 SetValue(GridFontHeightProperty, _fontSize); 

但它不起作用。在這種情況下,它不會正確設置字體高度,它只是使用數據網格的默認字體高度。

+0

我還以爲是出結合正常的CLR屬性。我創建一個綁定對象,設置源代碼然後以編程方式調用SetBinding。 – WPFNewbie

回答

1

首先,你不能綁定到一個私有變量。我的猜測是,你的變量_fontSize是private double,對吧? (請參閱我如何猜測?)) 您可以綁定到公共屬性或綁定到依賴屬性,在您的情況下它很合適。因此,創建一個名爲FontSize的新的依賴項屬性並綁定到該屬性。

如果由於某種原因,你不能用一個依賴屬性,你仍然可以綁定到使用INotifyPropertyChanged這應該是這個樣子

public double FontSize 
{ 
    get{return _fontSize;} 
    set 
    { 
     _fontSize = value; 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs("FontSize")); 
     } 
    } 
} 
+0

更新我的問題,以反映我嘗試使用依賴性屬性,但無效。 – WPFNewbie

0

你沒有正確地約束,你也不能(嚴格地說)你想要什麼。

綁定是總是針對普通代碼屬性或針對依賴項屬性。你不能綁定到字段。你可以這樣做:

首先,刪除_fontSize實例變量。你不會再使用它了。

下一步,將一個DependencyProperty此值:

private const string FontSizePropertyName = "FontSize"; 
    private static readonly DependencyProperty FontSizeProperty = 
    DependencyProperty.Register(
     FontSizePropertyName, 
     typeof(double), 
     typeof(ExtendedDataGridTextColumn), 
     new UIPropertyMetadata(0)); 
    private double FontSize 
    { 
     get { return (double)GetValue(FontSizeProperty); } 
     set { SetValue(FontSizeProperty, value); } 
    } 

然後改變你的綁定到這一點:

Binding binding = new Binding(); 
    binding.Source = RelativeSource.Self; 
    binding.Path = new PropertyPath(FontSizeProperty) 
    textBlockFNFactory.SetBinding(TextBlock.FontSizeProperty, binding); 

然後,只需使用新的FontSize屬性設置值,而不是更新的私人領域。

注:你是密切與您現有的依賴屬性,但PropertyPath只需string如果屬性是CLR(普通)屬性。對於依賴項屬性,您應該將實際的DependencyProperty實例傳遞給它,如上所示。

+0

最後一行參數new UIPropertyMetadata(0)會導致引發異常,如果我將其移除並不是例外,但它不起作用。由於某種原因,它也會減慢重繪速度。也許我需要給你一個大樣本,裏面有實際的類。 – WPFNewbie

+0

@WPFNewbie:試試'0.0'而不是'0'。 –

+0

這段代碼應該去哪裏?目前它在我的自定義數據網格類中,它創建了單個數據列。我嘗試將它全部移到ExtendedDataGridTextColumn類中,這使得它非常緩慢,就像10秒鐘渲染其中包含20條記錄的數據網格,並且它仍然不尊重綁定。 – WPFNewbie

0

我有幾個關於這個輸入...

  1. ExtendedDataGridTextColumnDataGridTextColumn延長若然你怎麼來使用CellTemplateDataGridTextColumn已將ElementStyleEditingElementStyle設置爲用其單元格表示的TextBlock和TextBox的樣式。

  2. 如果你確信你的決定在1點左右CellTemplate上方,然後嘗試....

    Binding binding = new Binding(); 
        binding.BindsDirectlyToSource = true; 
        binding.Source = _fontSize; 
        textBlockFNFactory.SetBinding(TextBlock.FontSizeProperty, binding); 
    

讓我知道如果任何這些幫助。

+0

+1對於BindsDirectlyToSource不確定它是否與新綁定(「。」)相同或甚至可能,並且忘記了該屬性。 – dowhilefor

+0

對於你的第一個問題,是的,它確實擴展了它。在我創建ExtendedDataGridTextColumn的代碼中,我動態地創建了幾個觸發器。這些配置在一個XML文件中,但它們可能是特定列上的20個觸發器中的很多個。我將這些觸發器添加到DataTemplate對象,然後將其分配給CellTemplate。如果我可以簡化這個,那就太好了。 – WPFNewbie

+0

對於第二個答案,它沒有什麼區別。正如dowhilefor指出的那樣,_fontSide是一個在類中聲明的私有double,創建了ExtendedDataGridTextColumn,而不是在ExtendedDataGridTextColumn類中。 – WPFNewbie