0

I am trying to set up an element within a WPF application, but cannot get the TextTrimming on the TextBlocks to work properly. This is within a ScrollViewer. I am currently using a DockPanel as the inner container.

I have searched quite a bit, but found no questions addressing this issue.

The XAML for the container:

<Grid>
  <ScrollViewer>
    <DockPanel Name="listedCharacters" LastChildFill="False"></DockPanel>
  </ScrollViewer>
</Grid>

The XAML for the child elements (added by code):

<UserControl …>
  <Grid DockPanel.Dock="Top" HorizontalAlignment="Stretch">
    <TextBlock Text="{Binding FullName}" TextTrimming="CharacterEllipsis" />
  </Grid>
</UserControl>

The first problem is that whether I use the DockPanel or a StackPanel, as the inner container, the child element's width appears to be dictated by its content (the TextBlocks) rather than constrained by the parent ScrollViewer.

The effect I want is for the ellipsis to truncate each TextBlock's content when the Window's grid column (not shown in code) is narrower than the bound text. Basically, a list that scrolls vertically when needed, and trims horizontally (which I thought would have been sufficiently common that the answer would be out there; alas, no).

I believe I need to use my own UserControl for this, as there is a lot more going on than shown her; right-click menus on the item in the list, etc.

The secondary issue, iff the optimal panel to use is the DockPanel, how to apply the DockPanel.Dock="Top" through code (C#) when the elements are dynamically added? Again, I cannot find anything that appears to explain this. (I know it is probably in the wrong place in the sample code above.)

Rick
  • 397
  • 3
  • 16
  • not sure I understand your problem but did you try to disable horizontal scrolling ``? – dkozl Jul 12 '14 at 13:15
  • Yes, I tried that. It didn't help (hence removed form the simplified code). All `HorizontalScrollBarVisibility="Disabled"` does is stop the scrollbar from displaying. It does not restrict the width of the inner content. – Rick Jul 12 '14 at 13:18
  • Try by setting width to textblock – Shivam cv Jul 12 '14 at 13:19
  • Explicitly setting a width will not work. The `ScrollViewer` is only in one column of the parent grid (again, not in sample code), and there is a `GridSplitter` that can change its width (the same effect, really, as changing the overall window width). Unless you are suggesting that I capture an event off the `GridSplitter`/Window and explicitly change the widths of all the child `TextBlock`s. – Rick Jul 12 '14 at 13:26
  • Oops. **Correction that first response**. I had previously tried `HorizontalScrollBarVisibility="Hidden"`, which didn't work. `HorizontalScrollBarVisibility="Disabled"` does work. (And with the `StackPanel`, which makes the other part redundant.) Thank you dkozl. – Rick Jul 12 '14 at 13:41
  • This is pretty common, you just need a way to invoke the trimming by setting some sort of boundary to do so. However as you say there's a lot more going on than shown there could be other elements at place through the tree causing your issue. Might share a more complete example instead of adhoc snippets to better diagnose your problem. – Chris W. Jul 12 '14 at 15:47
  • there seems to be some other element or properties apart from what we can see in the question which may be causing the issue. could you post a working sample which can reproduce the issue? may we have a look at whole picture. – pushpraj Jul 13 '14 at 03:34
  • As per my correction response to **dkozi**'s suggestion, it was indeed the `HorizontalScrollBarVisibility="Disabled"` that was needed. – Rick Jul 13 '14 at 16:33

0 Answers0