1

I am trying to create content inside of a Scrollbar in a Dialog. I am doing this through C# instead of XAML even though this is Win UI 3. The problem I am experiencing is that my Scrollbar is not on the right side of the dialog instead it is covering content.

This is what my code does right now

This is the my current code that makes the scrollbar:

StackPanel contentPanel = new StackPanel()
{
    Orientation = Orientation.Vertical,
    Children =
    {
new TextBlock
{
    Text = "Choose a color then select Save.",
    Margin = new Thickness(0, 0, 0, 10)
},

new ColorPicker
{
    ColorSpectrumShape = ColorSpectrumShape.Ring,
    // Other settings...
}
    }
};

ScrollViewer scrollViewer = new ScrollViewer
{
    Content = contentPanel,
    VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
    HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
    VerticalAlignment = VerticalAlignment.Stretch,
    HorizontalAlignment = HorizontalAlignment.Right,
};

ContentDialog dialog = new()
{
    Title = "Choose a color",
    Content = scrollViewer,
    PrimaryButtonText = "Save",
    CloseButtonText = "Discard",
    DefaultButton = ContentDialogButton.Primary,
    XamlRoot = this.Content.XamlRoot,
};

This is how I want it to look

Notice how the Scrollbar is touching the side. This what I am trying to do.

I had searched online for help but I did not find help to this issue.

I tried different ways of aligning the Scrollbar but none of those ways worked.

I had expected VerticalAlignment = VerticalAlignment.Stretch to align the Scrollbar the the right side of the dialog. This did not work.

8
  • You should be focusing on the "Content" of ScrollViewer ... you're "wrapping" something ("contentPanel") which has no apparent relation to "dialog" (the "content" in how you "want it") Commented Feb 10 at 19:01
  • @GerrySchmitz, I edited it to include 'contentPanel'. Commented Feb 10 at 20:55
  • Why are you aligning the ScrollViewer "right"? Every little bit "hurts". (Try) Give the StackPanel a right Margin or the ScrollViewer a right Padding. (0,0,x,0) Commented Feb 10 at 23:14
  • I think the problem is that there is a range that content can be in inside of the dialog. I need the scroll bar outside of that range so that it can be on the edge of the dialog. Commented Feb 11 at 1:51
  • Yes ... A ContentDialog has a maximum width and height. ("Device independence"). Commented Feb 11 at 2:18

3 Answers 3

0

It seems that the issue occurs because you align the entire ScrollViewer to the right instead of allowing it to stretch to fill the available space. Just remove HorizontalAlignment.Right and make sure that both alignments are set to Stretch. Also, if your contentPanel is a StackPanel, replace it with a Grid (or set HorizontalAlignment on the panel) to ensure that content stretches properly.

Here is fixed version:

 ScrollViewer scrollViewer = new ScrollViewer{
    Content = contentPanel,
    VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
    HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled, // Disable horizontal scroll
    VerticalAlignment = VerticalAlignment.Stretch,
    HorizontalAlignment = HorizontalAlignment.Stretch // Key change
};

ContentDialog dialog = new()
{
    Title = "Choose a color",
    Content = scrollViewer,
    // ... rest of your dialog properties
}; 
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to Gerry Schmitz I was able to load content outside of the dialog's content area using scaling.

double size = 1.05;

ScaleTransform adjustsize = new ScaleTransform
{
    ScaleX = size,
    ScaleY = size,
};

scrollViewer.RenderTransform = adjustsize;

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
It is appreciated to share your answer here. Please don't forget to accept your answer since the it works for you.
0

Try adjusting the ScrollViewer's Padding:

double scrollbarSize = (double)App.Current.Resources["ScrollBarSize"];
Thickness scrollViewerPadding = scrollViewer.Padding;
scrollViewerPadding.Right = scrollbarSize;
scrollViewer.Padding = scrollViewerPadding;

You can learn more about ScrollBarSize in the generic.xaml file.

UPDATE

If you want the Scrollbar to be at the right edge of the ContentDialog:

  • Stretch the ScrollViewer to fit in the ContentDialog.

    scrollViewer.HorizontalAlignment = HorizontalAlignment.Stretch;
    
  • Update the ContentDialog's Padding (which is 24 by default).

    if (dialog.Resources.TryGetValue("ContentDialogPadding", out var value) is true &&
      value is Thickness contentDialogPadding)
      {
          contentDialogPadding.Right = 0;
          dialog.Resources["ContentDialogPadding"] = contentDialogPadding;
      }
    

2 Comments

That does work partially... although the scroll bar is no longer covering content, it is still not on the edge of the dialog.
Updated my answer. I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.