问题
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