最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Why is a ScrollViewer created with code not aligned properly inside of a dialog? - Stack Overflow

programmeradmin5浏览0评论

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:

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. This is my current code:

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,
};

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

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:

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. This is my current code:

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,
};

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

Share Improve this question edited 6 hours ago Andrew KeepCoding 13.3k2 gold badges19 silver badges35 bronze badges asked 6 hours ago Stack Overflow UserStack Overflow User 113 bronze badges New contributor Stack Overflow User is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

2 Answers 2

Reset to default 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
}; 

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.

发布评论

评论列表(0)

  1. 暂无评论