Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ! So i am creating a WPF application using the MVVM architectural pattern. Inside this application i have a listview. I have binded the SizeChanged event of the window to a command using this piece of XAML code:
<i:EventTrigger EventName="SizeChanged">
            <i:InvokeCommandAction Command="{Binding WindowSizeChangedCommand}"/>
        </i:EventTrigger>


Now, what i want is to pass to this command a parameter, which is the listview width.

I tried to to this by the following code:

<i:EventTrigger EventName="SizeChanged">
            <i:InvokeCommandAction Command="{Binding WindowSizeChangedCommand}" CommandParameter="{Binding Width, ElementName=listView}"/>
        </i:EventTrigger>

The thing is that it doesn't work ! Hhahahahaha I know how to find an ancestor object with XAML. But i don't know how to do the opposite. Any ideas what to do ?? THank you very much for your time !

What I have tried:

<pre><i:EventTrigger EventName="SizeChanged">
            <i:InvokeCommandAction Command="{Binding WindowSizeChangedCommand}" CommandParameter="{Binding Width, ElementName=listView}"/>
        </i:EventTrigger>
Posted
Updated 18-Apr-17 10:26am
Comments
J. Calhoun 18-Apr-17 16:03pm    
What does your WindowSizeChangedCommand look like? Also, a tip, there was an issue that CommandParameter needed to be declared before the Command, I am not sure if that issue was resolved, or what version you are using but it is worth a try... but with command paramater declared first the relay should have something like "new RelayCommand(param => WindowSizeChange(param)); the param would be the CommandParameter that you passed in.

Also, I found that using width gave me NaN, but the property "Actual Width" gave me the actual number.

1 solution

<i:EventTrigger EventName="SizeChanged">
            <i:InvokeCommandAction Command="{Binding WindowSizeChangedCommand}" CommandParameter="{Binding ElementName=listView, Path=ActualWidth}"/>
        </i:EventTrigger>


C#
public RelayCommand WindowSizeChangedCommand
{
    get
    {
        if (WindowSizeChangedCommand == null)
                WindowSizeChangedCommand = new RelayCommand(param => WidowSizeChanged(param));

            return WindowSizeChangedCommand;
    }
}

public void WindowSizeChanged(object param)
{
    int width = Convert.ToInt32(param);
}


I tried this exact code putting the event trigger under the <window> tag, and it works for me.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900