Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass UIElement to CommandParameter

I'm using MVVM Light in my WPF application.

I created a class RedirectToUriCommandArgument.cs.

public class RedirectToUriCommandArgument : DependencyObject
{
    #region Properties

    public static readonly DependencyProperty PageProperty =
        DependencyProperty.Register(nameof(Page), typeof(object), typeof(RedirectToUriCommandArgument), new UIPropertyMetadata(null));

    public object Page
    {
        get => (object)GetValue(PageProperty);
        set => SetValue(PageProperty, value);
    }

    public string Uri { get; set; }

    #endregion

    #region Methods



    #endregion
}

In .xaml file, I used:

<Window x:Class="MainClient.Views.AppView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:v="clr-namespace:MainClient.Views"
        xmlns:vm="clr-namespace:MainClient.ViewModel"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:commandArgument="clr-namespace:MainClient.Models.CommandArguments"
        xmlns:local="clr-namespace:MainClient"

        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Height="350" Width="525">

    <Window.DataContext>
        <vm:AppViewModel x:Name="AppContext"></vm:AppViewModel>
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="32"/>
        </Grid.RowDefinitions>

        <Frame NavigationUIVisibility="Hidden" x:Name="PageFrame">
            <Frame.Content>
                <Page Name="MainPage"></Page>
            </Frame.Content>
        </Frame>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button>
                <Button.Content>
                    <TextBlock>Redirect to main view</TextBlock>
                </Button.Content>

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <i:InvokeCommandAction Command="{Binding RedirectToViewRelayCommand}">
                            <i:InvokeCommandAction.CommandParameter>
                                <commandArgument:RedirectToUriCommandArgument Page="{Binding ElementName=PageFrame}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>
                            </i:InvokeCommandAction.CommandParameter>
                        </i:InvokeCommandAction>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </StackPanel>
    </Grid>
</Window>

The Page property is always null.

Am I missing anything ?

like image 889
Redplane Avatar asked Aug 15 '26 07:08

Redplane


1 Answers

So I think the problem is, that as binding been initialized the UIElement is not created(null). Afterwords the binding is not notified, that the object is created.

Binding to the properties is easyer the object must implement INotifyPropertyChanged or DependencyObject take care about dependency properties.

To solve your issue you could set a Delay for Binding, say to 1000ms, then it will work. It's doubtful whether it is a right way.

<commandArgument:RedirectToUriCommandArgument Page="{Binding ElementName=PageFrame, Delay=1000}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>

The right way would be just set binding's source to the UIElement:

<commandArgument:RedirectToUriCommandArgument Page="{Binding Source={x:reference PageFrame}}" Uri="MainView.xaml"></commandArgument:RedirectToUriCommandArgument>
like image 188
Rekshino Avatar answered Aug 16 '26 22:08

Rekshino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!