1

I have a user control that is displayed in a popup dialog using Prism's PopupWindowAction. I don't want the window to be resizeable. Is it possible to style this window from the user control? I attempted to use this:

<UserControl.Resources>
    <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
        <Setter Property="ResizeMode" Value="NoResize" />
    </Style>
</UserControl.Resources>

but it is not working.

Edit:

Based on the accepted answer, I moved the style over to the user control that is defining the IteractionRequestTrigger and assigning the PopupWindowAction's WindowStyle.

New code in calling use control:

Add Resource

<UserControl.Resources>
    <Style x:Key="WindowStyle" TargetType="Window">
        <Setter Property="ResizeMode" Value="NoResize" />
        <Setter Property="SizeToContent" Value="WidthAndHeight" />
    </Style>
</UserControl.Resources>

Popup Window declaration

<prism:InteractionRequestTrigger SourceObject="{Binding InteractionRequest}">
    <prism:PopupWindowAction WindowStyle="{StaticResource WindowStyle}">                
        <prism:PopupWindowAction.WindowContent>
            <sharedV:InformationDialog />
        </prism:PopupWindowAction.WindowContent>
    </prism:PopupWindowAction>
</prism:InteractionRequestTrigger>
Jesse
  • 915
  • 1
  • 11
  • 20

2 Answers2

2

The reason why your XAML doesn't work is:

  • The style you created is explicit - it has a x:Key. Explicit styles have to be applied on the target element directly <Window Style="{StaticResource WindowStyle}" ... />
  • Even if you remove the x:Key and make it an implicit style, since it is defined in the UserControl's resources, it will only apply to items below the UserControl in the tree.

In this case you might want to look at the WindowStyle property of the PopupWindowAction. You should be able to set the style there.

Keithernet
  • 2,349
  • 1
  • 10
  • 16
1

Children cannot directly change their parent's style since the visual tree model is designed to go from broad to specific (Window to UserControl...) with styles being inherited and overridden in that direction. That said, anything is possible because it's all just code!

This isn't a nice way to do it, but you can use the Loaded method of your UserControl in the code behind to do the work of navigating the visual tree to find the parent Window and forcibly set the ResizeMode property. You can do this using this.Parent and checking for when is Window is true, or you can use VisualTreeHelper.GetParent.

XAML:

<UserControl Loaded="OnLoaded"></UserControl>

C#:

private void OnLoaded(object sender, RoutedEventArgs e)
{
    var currentParent = Parent;

    while (currentParent != null && !(currentParent is Window))
    {
        currentParent = VisualTreeHelper.GetParent(currentParent);
    }

    if (currentParent is Window parentWindow)
    {
        parentWindow.ResizeMode = ResizeMode.NoResize;
    }
}
Will Custode
  • 4,576
  • 3
  • 26
  • 51
  • I was thinking that the answer would be something along these lines. I'll give it a try. Thanks for your help. – Jesse Nov 21 '19 at 12:52
  • I chose the answer with no code behind, but since I'm sure this would work, I gave you an upvote. – Jesse Nov 21 '19 at 20:00