Quantcast
Channel: patterns & practices: Prism
Viewing all articles
Browse latest Browse all 1878

New Post: (WPF4+ Prism4) How to use InvokeCommandAction ?

$
0
0
I have a TreeView with HierarchicalDataTemplate. In the leaf node of the treeview, I have a button
binding to a command object, which have done the job as I expected.

Here is the working Xaml code:

<UserControl x:Class="YhControls.Views.ListFilesView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:m="clr-namespace:YhCommon;assembly=YhCommon"
         xmlns:vm="clr-namespace:YhControls.ViewModels"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid Name="root">
    <Grid.Resources>
        <HierarchicalDataTemplate DataType = "{x:Type vm:Node}"  ItemsSource = "{Binding Path=nodes}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType = "{x:Type vm:Leaf}"  ItemsSource = "{Binding Path=nodes}" >
            <Button Content="{Binding Path=Name}" Command="{Binding ElementName=root,Path=DataContext.ShowFileCommand}" CommandParameter="{Binding }" />

        </HierarchicalDataTemplate>
    </Grid.Resources>
.....
</UserControl>

Now I want change the implementation from "Binding Commands" to "InvokeCommandAction".

Here is the version of "InvokeCommandAction":

<UserControl x:Class="YhControls.Views.ListFilesView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
         xmlns:m="clr-namespace:YhCommon;assembly=YhCommon"
         xmlns:vm="clr-namespace:YhControls.ViewModels"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid Name="root">
    <Grid.Resources>
        <HierarchicalDataTemplate DataType = "{x:Type vm:Node}"  ItemsSource = "{Binding Path=nodes}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType = "{x:Type vm:Leaf}"  ItemsSource = "{Binding Path=nodes}" >
            <Button Content="{Binding Path=Name}"  >
                    <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click" >
                        <i:InvokeCommandAction Command="{Binding ElementName=root,Path= DataContext.ShowFileCommand}" CommandParameter="{Binding }"  />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </HierarchicalDataTemplate>

    </Grid.Resources>
...
</UserControl>

But, in "InvokeCommandAction" version, I got exception:
"Baml stream has unexpected record. Try to add to InvokeCommandAction,
but it is not a collection or which has TypeConverter."

Hope some one can help me.

Viewing all articles
Browse latest Browse all 1878

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>