lundi 8 janvier 2018

Binding EventTrigger Command to DataContext of UserControl in Resource Dictionary Item Template

I have a UserControl which has a ListView in it. The ItemTemplate for the ListView is defined in my RawMaterialResource.XAML (a resource dictionary declared as such in the user control).

<UserControl
    x:Class="SawManager.UserControls.SawCutListUC"
             xmlns="http://ift.tt/o66D3f"
             xmlns:x="http://ift.tt/mPTqtT"
             xmlns:mc="http://ift.tt/pzd6Lm" 
             xmlns:d="http://ift.tt/pHvyf2" 
    xmlns:i="http://ift.tt/1hg9DTd"
             xmlns:local="clr-namespace:SawManager.UserControls"
             xmlns:ViewModels="clr-namespace:SawManager.ViewModels"
             mc:Ignorable="d" 
             d:DesignHeight="480" d:DesignWidth="1280">

    <UserControl.DataContext>
        <ViewModels:SawCutListUCViewModel/>
    </UserControl.DataContext>
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisCon"/>
        <CollectionViewSource Source="{Binding}" x:Key="SawCutListView">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="RawMatPartNum"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </UserControl.Resources>

    <Grid Background="Transparent">
        <Grid.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/Resources/RawMaterialResource.xaml"/>
                    <ResourceDictionary Source="/Resources/OtherOpenItemsResource.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Grid.Resources>

This is where I am using the Resource Dictionary (RawMaterialTemplate) in my UserControl as an ItemsTemplate for a ListView:

 <ListView Name="MainListView" Background="#454647"
              ItemsSource="{Binding SawCutCollectionView}" 
              ItemTemplate="{StaticResource RawMaterialTemplate}"
              HorizontalContentAlignment="Stretch" Grid.Row="2"
              ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding LoadOtherItems}"
                                       CommandParameter="{Binding ElementName=MainListView, Path=SelectedItem}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListView>

NTOE:The EventTrigger for the ListView here in the UserControl is working perfectly. Do not want to confuse other into thinking that's where I'm having issues.

Here is the top of the ResourceDictionary:

<ResourceDictionary xmlns="http://ift.tt/o66D3f"
                    xmlns:x="http://ift.tt/mPTqtT"
                    xmlns:i="http://ift.tt/1hg9DTd"
                    xmlns:models="clr-namespace:SawManager.Models"
                    xmlns:local="clr-namespace:SawManager.Resources"
                   >
    <local:CurrentJobMultiConverter x:Key="CurrentJobMultiConverter"/>
    <DataTemplate x:Key="RawMaterialTemplate" DataType="{x:Type models:SawCutListItem}">

and here is where I am trying to use the EventTrigger in the Resource Dictionary:

    <CheckBox  Command="{Binding SawQueue94}" CommandParameter="{Binding}" Grid.Column="3">
        <i:EventTrigger EventName="Checked">
            <i:InvokeCommandAction Command="{Binding DataContext.UpdatePartQueues, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}"/>
        </i:EventTrigger>
    </CheckBox>
    <CheckBox Command="{Binding SawQueue97}" Grid.Column="4">
        <i:EventTrigger EventName="Checked">
            <i:InvokeCommandAction Command="{Binding DataContext.UpdatePartQueues, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" CommandParameter="{Binding}"/>
        </i:EventTrigger>
    </CheckBox>

So, these checkboxes will toggle a boolean property (SawQueue94 and SawQueue97 respectively) in the class model, SawCutListItem. At the same time, I would like to fire a command in the ViewModel of my UserControl, which will add this particular class object to a list of parts to queue for a saw. which list will be determined by the aforementioned boolean properties in the SawCutListItem class.

YES, I do already have these objects in an ObservableCollection, but I am trying to avoid running all 200+ objects through a foreach loop when it comes time to update the corresponding records in our database, so a 'partsToQueueInSaw' collection of some sort is my current solution to that.

This is why I believe I need BOTH the command to toggle the boolean in the CheckBox tag AND an EventTrigger on each CheckBox. The crux of this solution is the command I want this EventTrigger to fire is in the ViewModel for the UserControl, not the SawCutListItem class. I tried to access this datacontext, as you can see from the code I gave where I'm using the EventTriggers, but this event is not being hit by my breakpoints when I check either CheckBox.

EDITIf I put this command in the tag for the checkbox, then the breakpoint for my DelegateCommand's function in my UserControl's ViewModel IS hit. Only when I put it in the EventTrigger does it not function.

How might I tell the EventTrigger for this CheckBox in this ResourceDictionary to look to the DataContext of the UserControl rather than the model?

I hope this was clear and enough detail was provided! Thanks very much for a solution!




Aucun commentaire:

Enregistrer un commentaire