WPF MaterialDesignInXamlToolkit Dialog expand to full width

妖精的绣舞 提交于 2021-02-11 15:14:44

问题


I'm trying to get the dialog in DialogHost to be horizontally stretched as much as possible, without hardcoding the size of the control that I'm displaying.

Tried the following:

  • setting the UserControl.HorizontalAligment to Stretch
  • setting HorizontalContentAligment to Stretch on DialogHost

I noticed that if set the Width to something like 6000, the dialog fills the DialogHost with some nice padding, but the content overflows and things on the right side are not visible.

Is there some standard way to achieve this?

I ended up doing this and it works, but maybe there is a better way

// in ControlShowedInDialog.xaml.cs
this.WhenActivated(d =>
    {
        var window = Window.GetWindow(this);
        window.WhenAnyValue(x => x.ActualWidth).Throttle(TimeSpan.FromMilliseconds(10), RxApp.MainThreadScheduler).Do(width => Width = width * 0.85).Subscribe().DisposeWith(d);
        window.WhenAnyValue(x => x.ActualHeight).Throttle(TimeSpan.FromMilliseconds(10), RxApp.MainThreadScheduler).Do(height => Height = height * 0.75).Subscribe().DisposeWith(d);

回答1:


Try using RelativeSource binding to bind with DialogHost's ActualWidth.

Set this to your UserControl,

...
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
...
Width="{Binding Path=ActualWidth, 
                RelativeSource={RelativeSource AncestorType={x:Type wpf:DialogHost}, 
                Mode=FindAncestor}}"

If you still need some more adjustments, you can use IValueConverter to achieve that.



来源:https://stackoverflow.com/questions/61088234/wpf-materialdesigninxamltoolkit-dialog-expand-to-full-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!