Bob.
Bob.

Reputation: 4002

Is there a code-behind equivalent for TextBlock ScrollViewer.CanContentScroll="True" for DataGridTemplateColumn?

For a TextBlock in XAML, you can do the following inside a DataTemplate:

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

But when I try to set ScrollViewer.HorizonalScrollBarVisibility, it doesn't seem to do anything.

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);

I am trying to make a DataGrid Column that has a TextBlock that shows one line of text, but allows you to scroll up/down to see the rest of the textblock.

Upvotes: 0

Views: 781

Answers (1)

John Bowen
John Bowen

Reputation: 24453

TextBlock doesn't have a ScrollViewer contained in it to set scrolling behavior on. You need to wrap it in a ScrollViewer on which you can set whatever you want. Contrast this to a ListBox, which does contain a ScrollViewer in its ControlTemplate so can take advantage of the attached properties.

Upvotes: 0

Related Questions