microsoft-ui-xaml: Unable to set width of ContentDialog
Describe the bug
Using the properties MinWidth, MaxWidth and Width have no effect on the width of the ContentDialog but break the ability to resize the window and keep the ContentDialog centered. There are numerous questions on StackOverflow about setting the content dialog width. The solution is always to change the ContentDialogMaxWidth in App.xaml but even setting that does not work as expected.
Steps to reproduce the bug Add a button to a page and use the following event handler:
private async void Button_Click(object sender, RoutedEventArgs e)
{
ContentDialog dialog = new ContentDialog();
string content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque varius vehicula orci. Integer erat lectus, congue in maximus ac, venenatis sed risus. Fusce odio nisl, luctus id rhoncus a, pretium eget dolor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas pellentesque cursus nibh, id venenatis lectus vulputate id. Curabitur ut ligula bibendum, vehicula tellus in, sodales leo. Suspendisse sed tellus imperdiet, ullamcorper libero non, venenatis enim. Quisque quis nibh bibendum, aliquam nunc vel, cursus erat.";
content += "\n\nIn eleifend placerat velit eget tempus. Duis eget augue tellus. Mauris fringilla vehicula cursus. Nunc maximus libero et nisi gravida vulputate. Sed consectetur id urna quis consectetur. Quisque id nulla venenatis dolor semper rhoncus. Ut metus erat, tincidunt ut consectetur vitae, suscipit a ligula. Etiam mollis nec nibh quis suscipit. Aliquam quis eleifend mauris. Nunc consequat id purus vitae efficitur. Fusce quis imperdiet nibh, eget mattis urna. In vitae ipsum commodo, feugiat neque non, tempus urna. Praesent cursus nisi et ex interdum, elementum ultricies nisi eleifend. Etiam metus lorem, vulputate a leo nec, molestie porttitor leo. Proin eget dictum orci.";
content += "\n\nNam pellentesque lacus orci, vel tempus turpis semper id. Mauris ut egestas mi, a facilisis ligula. Aenean dapibus nisi eget sapien varius, et suscipit turpis imperdiet. Aenean blandit ornare porta. Praesent hendrerit, ligula at ullamcorper euismod, magna enim venenatis neque, ac tincidunt magna elit in eros. Nulla viverra consectetur faucibus. Etiam tincidunt ornare tortor vitae viverra. Quisque a risus in ligula sagittis tempus. Maecenas consequat lectus ornare odio sagittis, quis feugiat quam facilisis. Quisque ac ante mauris. Sed convallis pellentesque lorem, a accumsan erat commodo eu. Vivamus malesuada pharetra purus, nec hendrerit sapien ultricies id. In rutrum mauris libero, id finibus urna imperdiet vitae. Sed ultrices ornare mi, eu eleifend dui tempor sed. Praesent nec ante mollis, ornare quam eget, sollicitudin eros.";
content += "\n\nCras lacinia nisi at velit tempus pretium. Duis quis enim mi. Nunc euismod, urna ut vulputate eleifend, tortor augue lacinia dui, eu pulvinar odio urna quis arcu. Morbi eget eleifend nibh, sit amet ultrices mi. Curabitur convallis urna ut neque dignissim malesuada. Morbi eget mollis nulla. Praesent vel libero placerat, rhoncus mi eget, auctor tortor. Integer eget ornare odio, vitae mattis diam. Praesent vestibulum varius eleifend. Quisque egestas, quam quis elementum pharetra, enim tortor scelerisque arcu, at posuere nisi eros sed ex. Etiam rutrum porttitor urna non vestibulum.";
dialog.Content = content;
dialog.MinWidth = this.ActualWidth * .8;
dialog.Width = this.ActualWidth * .8;
dialog.MaxWidth = this.ActualWidth * .8;
await dialog.ShowAsync();
}
When updating the ContentDialogMaxWidth:
...
<Application.Resources>
<x:Double x:Key="ContentDialogMaxWidth">9999</x:Double>
</Application.Resources>
...
Expected behavior I would expect the following behaviour:
- (Without overriding
ContentDialogMaxWidth) The dialog consumes 80% of the window size - Resizing the window keeps the ContentDialog horizontally centered
Actual behaviour
- When setting MinWidth/MaxWidth/Width, the ContentDialog is no longer centered (see screenshot 2)
- When setting MinWidth/MaxWidth/Width properties, the ContentDialog is no longer repositioned and recentered when the window is resized (see screenshot 3)
- When setting
ContentDialogMaxWidth, in conjunction with MinWidth/MaxWidth/Width 80% of the width is used however the dialog is not horizontally centered and does not recenter as the dialog is resized.
Screenshots Screenshot 1: None of MinWidth/MaxWidth/Width are set:

The following screenshots have MinWidth/MaxWidth and Width set to this.ActualWidth * .8
Screenshot 2: ContentDialog is no longer centered:

Screenshot 3: ContentDialog is not repositioned on resize:

The following screenshots have MinWidth/MaxWidth and Width set to this.ActualWidth * .8 with ContentDialogMaxWidth = 9999

Version Info 1809 using the Windows inbuilt controls
NuGet package version: N/A
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 16
- Comments: 38 (4 by maintainers)
Commits related to this issue
- ContentDialog width does not work well... Note: https://github.com/microsoft/microsoft-ui-xaml/issues/424 Maybe? — committed to kurema/BookViewerApp3 by kurema 5 years ago
Any updates? 2022 is coming but the ContentDialog (the rectangle with content) still can’t measure width by content.
Hi everyone. I also struggled with this a while ago and i don’t remember where i saw this but this problem has been “solved” a while ago. This has to do with the order of resource loading. By default when installing WinUI nuget or creating a new Windows App SDK project, the App.xaml file is like this:
problem is when you put your
<x:Double x:Key="ContentDialogMaxWidth">9999</x:Double>in the other app resources, somehow it gets loaded before the WinUI styles. and so it’s ignored. To solve this, the WInUI team created a way to ensure their resources are loaded first and then yours so you can override them.Notice we now import the namespace xmlns:controls=“using:Microsoft.UI.Xaml.Controls” and then our overrides go into a special “controls:XamlControlsResources” This works flawlessly.
hahh,i test a new method, is works for me
dialog.Resources["ContentDialogMaxWidth"] = 1080;My workaround as blow, instead of changing the width of the ContentDialog, I changed the with of the root element in the dialog.
Any updates on this issue? I managed to make mine work using the above mentioned workarounds but I really would like to see this bug getting fixed.
Is this still on the radar, to have a functioning modal solution for a windowing operating system? It is absolutely insane that the implementation of ContentDialog is so broken that you cannot properly resize or reposition them. The current implementation works fine for a message box replacement but is absolutely unworkable for any other form of modal task (preferences window, data entry, etc).
I’m hopeful that this can be addressed soon.
Thank you SO much for this. This problem was driving me crazy. Works flawlessly in WinUI3, SDK 1.5.
Setting
<x:Double x:Key="ContentDialogMaxWidth">800</x:Double>inapp.xamlon Windows 10 1909, worked for me.Are there any updates? The workaround works but the issue still persists in 1.7.1.
FYI, this is fixed in Windows App SDK 1.1.4
You can now go back to using this as you typically would:
+1 for me too. When ContentDialog is called from a Window with ExtendsContentIntoTitleBar = true , it is impossible to drag it around .
I tried several suggestions and the only thing that worked for me was setting the above mentioned double key in the resources of the content dialog itself. (It did not work in the App.xaml for me).
Good bug. I believe this is because ContentDialog is a FrameworkElement but it moves its content to a popup so the layout-related properties don’t take effect. We can probably make the *Width/*Height properties work.