开发者

WPF: Problems with Command when put a ListView inside an Accordion using MVVM Light

I have this View:

<Grid Margin="10,0,10,10">
<StackPanel>
    <wpftoolkit:Accordion 
    Margin="4" 
    HorizontalAlignment="Stretch" 
    Name="accordionCB"
    ItemsSource="{Binding Path=ContentBlockCategories}"
    SelectionMode="ZeroOrMore">
        <wpftoolkit:Accordion.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Stretch" Margin="0 0 0 4">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <TextBlock 
                            Text="{Binding Description}"
                            Grid.Column="0" VerticalAlignment="Center" />
                </Grid>
            </DataTemplate>
        </wpftoolkit:Accordion.ItemTemplate>
        <wpftoolkit:Accordion.ContentTemplate>
            <DataTemplate>

                <!--Not Raise MouseDoubleClick Command-->

                <ListView 
                x:Name="lvContentBlocks"
                AlternationCount="2" 
                ItemsSource="{Binding ContentBlocks}"
                IsSynchronizedWithCurrentItem="True"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding Path=MouseDoubleClick}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0,0,0,5">
                                <StackPanel VerticalAlignment="Top">
                                    <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </wpftoolkit:Accordion.ContentTemplate>
    </wpftoolkit:Accordion>

    <!--Raise MouseDoubleClick Command-->

    <ListView 
                x:Name="lvContentBlocks"
                AlternationCount="2" 
                ItemsSource="{Binding ContentBlockCategories}"
                IsSynchronizedWithCurrentItem="True"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <cmd:EventToCommand Command="{Binding MouseDoubleClick}" PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0,0,0,5">
                    <StackPanel VerticalAlignment="Top">
                        <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackPanel>
</Grid>

And this ViewModel:

public class MainViewModel : ViewModelBase
    {
        public ObservableCollection<ContentBlockCategory> ContentBlockCategories
        {
            get
            {
                return new ObservableCollection<ContentBlockCategory>
                    {
                        new ContentBlockCategory{
                            Id = 1,
                            Description = "Category 1",
                            ContentBlocks = new Collection<ContentBlock>
                            {
                                new ContentBlock{
                                    Id = 1,
                                    Description = "Content Block 1",
                                    Text = "Text"
                                }
                            }
                        }
                    };
            }
            set
            {
                RaisePropertyChanged("ContentBlockCategories");
            }
        }

        public RelayCommand<MouseButtonEventArgs> MouseDoubleClick
        {
            get;
            private set;
        }

        public MainViewModel()
        {
            MouseDoubleClick = new RelayCommand<MouseButtonEventArgs>(e =>
            {
                DependencyOb开发者_JAVA技巧ject dep = (DependencyObject)e.OriginalSource;

                while ((dep != null) && !(dep is ListViewItem))
                {
                    dep = VisualTreeHelper.GetParent(dep);
                }

                if (dep == null) return;

                ContentBlockCategory contentBlockSelected = (ContentBlockCategory)((ListViewItem)dep).DataContext;
            });
        }
    }

The first ListView inside an Accordion not raise the RelayCommand in the ViewModel but the second ListView raise the RelayCommand without any problem.

What's the problem?

Regards


I've had issues with EventToCommand in Templates and Styles an example of the problem can be found here

I have moved to AttachedCommandBehaviors (ACB) in these situations. The link takes you to the C# Diciples on how to use ACB.

A code snip of it's use from my code base:

            <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <StackPanel x:Name="IconBorder"
                               acb:CommandBehavior.Event="PreviewMouseUp"
                               acb:CommandBehavior.Command="{Binding Path=DataContext.ClickCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                               acb:CommandBehavior.CommandParameter="{Binding}">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜