2013-05-09 83 views
0

對於XAML TextBlock中,你可以做以下的一個DataTemplate內:DataGridTemplateColumn是否存在TextBlock ScrollViewer.CanContentScroll =「True」的代碼隱藏等價物?

<TextBlock Text="myTextBlock Text" VerticalAlignment="Center" Margin="0,0,5,0" 
ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible"/> 

但是,當我嘗試設置ScrollViewer.Horizo​​nalScrollBarVisibility,它似乎並沒有做任何事情。

DataTemplate textBlockTemplate = new DataTemplate(); 
FrameworkElementFactory textBlockElement = new FrameworkElementFactory(typeof(TextBlock)); 
Binding c1Binding = new Binding("myBindingValue") { Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }; 
textBlockElement.SetBinding(TextBlock.TextProperty, c1Binding); 
textBlockElement.SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap); 
textBlockElement.SetValue(TextBlock.HeightProperty, System.Convert.ToDouble(23)); 

textBlockElement.SetValue(ScrollViewer.CanContentScrollProperty, true); 
textBlockElement.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Visible); 
textBlockTemplate.VisualTree = textBlockElement; 
templateColumn.CellTemplate = textBlockTemplate; 
myDataGrid.Columns.Add(templateColumn); 

我試圖讓有一個TextBlock顯示一行文字DataGrid列,但可以讓你向上/向下滾動看到文本塊的其餘部分。

回答

0

TextBlock沒有包含在其中的ScrollViewer來設置滾動行爲。您需要將其包裝在ScrollViewer中,您可以在其中設置任何你想要的。將其與ListBox進行對比,其ControlTemplate確實包含ScrollViewer,因此可以利用附加的屬性。

相關問題