The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
MenuFlyout MenuFlyoutItem Command DataBinding with "sender" as parameter RRS feed

  • Question

  • Hello,

    Ich versuche folgendes Problem zu lösen und benötige etwas Hilfe (PRISM ist implementiert):

    I am trying to solve a problem and Need a Little help:

    I have a ListBox databind to an ObservableCollection. Adding entries is working perfectly. But now I want to be able to change, delete and move them utilizing a MenuFlyout connected to the respective item.

    XAML is as follows:

    <controls:PageBase.Resources>
            <DataTemplate x:Key="CheckListEntry">
                <Grid Width="{Binding ActualWidth, ElementName=ContentPanel, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="Auto" Holding="Grid_Holding">
                    <FlyoutBase.AttachedFlyout>
                        <MenuFlyout>
                            <MenuFlyoutItem x:Name="Edit" Text="Edit"/>
                            <MenuFlyoutItem x:Name="Delete" Text="Delete" Command="{Binding DeleteCommand}"/>
                            <MenuFlyoutItem x:Name="MoveUp" Text="Move Up" Command="{Binding MoveUpCommand}"/>
                            <MenuFlyoutItem x:Name="MoveDn" Text="Move Down" Command="{Binding MoveDownCommand}"/>
                        </MenuFlyout>
                    </FlyoutBase.AttachedFlyout>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="7*"/>
                        <ColumnDefinition Width="1*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding Item, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Foreground="White" FontSize="24" VerticalAlignment="Center"/>
                    <CheckBox Grid.Column="1" IsChecked="{Binding Checked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Command="{Binding SaveCollectionToFile}"/>
                </Grid>
            </DataTemplate>
        </controls:PageBase.Resources>

    C# command is as follows:

    private DelegateCommand<CheckListItem> _deleteCommand;
    public DelegateCommand<CheckListItem> DeleteCommand
            {
                get
                {
                    if (_deleteCommand != null)
                        return _deleteCommand;
                    _deleteCommand = new DelegateCommand<CheckListItem>
                        (
                            (item) =>
                            {
                                Collection.Remove(item);
                            }
                        );
                    return _deleteCommand;
                }
            }
    Does anyone know what to pass as a command paramenter to geht the CheckListItem in the ViewModel and whether DelegateCommand<CheckListItem> is correct or rather use object with casting or type conversion?

    Saturday, September 12, 2015 2:13 PM

Answers

  • Hello c0d3_m,

    >>Does anyone know what to pass as a command paramenter to geht the CheckListItem in the…

    Since you are using the Command, you could pass an argument by the CommandParameter property. And since its type is object, you could pass any type you want to it(primitive types or reference types).

    For example:

    <Button Command="{Binding SaveCommand}" CommandParameter="{Binding User}" Foreground="Wheat"  Width="100" Content="Login" ></Button>

    The “User” is a custom defined reference type. For details, you could have a check this thread:

    https://social.msdn.microsoft.com/Forums/windowsapps/en-US/892446da-8630-4877-8073-48cecf2fd6a5/windows-8-app-with-command-save-form-mvvm?forum=wpdevelop

    By the way, please also check this guide post to add a proper tag for the thread title.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Fred Bao Tuesday, September 22, 2015 1:06 PM
    • Marked as answer by Fred Bao Wednesday, September 23, 2015 10:26 AM
    Monday, September 14, 2015 3:03 AM

All replies

  • Hello c0d3_m,

    >>Does anyone know what to pass as a command paramenter to geht the CheckListItem in the…

    Since you are using the Command, you could pass an argument by the CommandParameter property. And since its type is object, you could pass any type you want to it(primitive types or reference types).

    For example:

    <Button Command="{Binding SaveCommand}" CommandParameter="{Binding User}" Foreground="Wheat"  Width="100" Content="Login" ></Button>

    The “User” is a custom defined reference type. For details, you could have a check this thread:

    https://social.msdn.microsoft.com/Forums/windowsapps/en-US/892446da-8630-4877-8073-48cecf2fd6a5/windows-8-app-with-command-save-form-mvvm?forum=wpdevelop

    By the way, please also check this guide post to add a proper tag for the thread title.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Fred Bao Tuesday, September 22, 2015 1:06 PM
    • Marked as answer by Fred Bao Wednesday, September 23, 2015 10:26 AM
    Monday, September 14, 2015 3:03 AM
  • Thank you Fred, that makes sense...

    But I run into another Problem, no command is executed when clicking the menuitem only Events in code-behind are getting fired. I tested the commands with the command bar (just for testing purposes) They are working properly.

    For some reason I don't understand and after doing Research for hours I couldn't find anything about it. They simply don't execute once they are in the menuitem of the item template.

    Monday, September 14, 2015 10:26 AM
  • >> no command is executed when clicking the menuitem only Events in code-behind are getting fired.

    This sounds strange, could you upload a demo project which could reproduce the new issue you encounter? We could try to help test it.

    PS: Since this is an new issue, it is suggested to ask it on a new thread and closed the original one if there is a proper answer.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Fred Bao Tuesday, September 22, 2015 1:06 PM
    Tuesday, September 15, 2015 9:36 AM