Home > Back-end >  XAML Passing Messages from another Element
XAML Passing Messages from another Element

Time:07-09

I have a button where I'm trying to pass down to the ViewModel the ActualHeight/ActualWidth of the main grid on the control. My XAML looks something like this:

  <Grid x:Name="MainGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="47" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!--  ROW 0  -->
        <StackPanel Orientation="Horizontal">
            <Label Content="{Binding ElementName=MainGrid, Path=ActualWidth}" />
            <Label Content="{Binding ElementName=MainGrid, Path=ActualHeight}" />

            <Button
                Width="20"
                Height="20"
                Margin="5,5,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                cm:Message.Attach="[Event Click] = [Action LaunchMirrorView({Binding ElementName=MainGrid, Path=ActualWidth}, {Binding ElementName=MainGrid, Path=ActualHeight})]"
                ToolTip="Expand View">
                <materialDesign:PackIcon
                    Background="Transparent"
                    Foreground="{StaticResource TealMidBrush}"
                    Kind="ArrowExpandAll" />
            </Button>
    ...

The two labels which show the "MainGrid's" ActualWidth & ActualHeight both have positive numbers showing 415 and 660, respectively. However, when I try to pass those numbers down to the ViewModel via the Caliburn Micro messaging of:

cm:Message.Attach="[Event Click] = [Action LaunchMirrorView({Binding ElementName=MainGrid, Path=ActualWidth}, {Binding ElementName=MainGrid, Path=ActualHeight})]"

What comes into my ViewModel's method always comes in a zero for both of those variables?

public void LaunchMirrorView(double actualWidth, double actualHeight)
{
  \\ actualWidth = 0
  \\ actualHeight = 0
}

Any ideas on how to pass those sizes correctly?

CodePudding user response:

You do not need to use the {Binding..} here. Following would be sufficient.

[Action LaunchMirrorView(MainGrid.ActualWidth,MainGrid.ActualHeight)]

Complete Code

<Button Content="Click" 
     cal:Message.Attach="[Event Click] = [Action LaunchMirrorView(MainGrid.ActualWidth,MainGrid.ActualHeight)]" />

This would ensure you receive the parameters ActualWidth and ActualHeight in the ViewModel

  • Related