Bind Button Command in ControlTemplate to SelectedItem from Element in DataTemplate

190 views Asked by At

I have done some googling and searches and find many questions on StackOverFlow that come close to helping me but all the ideas from them still give me binding expression errors in one form or another with most always mentioning it cannot find the source.

Cannot find source for binding with reference 'ElementName=filesList'.

View that calls control template:

<Expander
                DockPanel.Dock="Top"
                SnapsToDevicePixels="True"
                Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="3" Grid.RowSpan="1"
                Header="Files"
                IsExpanded="True"
                ExpandDirection="Down"
                Background="{DynamicResource DynamicFrmBG}" Foreground="{DynamicResource DynamicFrmFG}"
                >
                <ContentControl
                    SnapsToDevicePixels="True"
                    Template="{StaticResource filesTemplate}"
                    />
            </Expander>

Control Template Code Snippet (Comment in this code is another example of different things I have tried):

<ControlTemplate x:Key="filesTemplate">
    <Grid
        SnapsToDevicePixels="True"
        >
        <Grid.RowDefinitions>
            <RowDefinition Height="5" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="5" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="5" />
        </Grid.ColumnDefinitions>

       <ContentControl
            SnapsToDevicePixels="True"
            Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="2" Grid.RowSpan="1"
            Margin="0,0,0,0"
            Content="{Binding Files}"
            ContentTemplate="{StaticResource filesListTemplate}"
            />

        <StackPanel
            SnapsToDevicePixels="True"
            Grid.Column="1" Grid.ColumnSpan="1" Grid.Row="3" Grid.RowSpan="1"
            Orientation="Horizontal"
            >
            <Button
                SnapsToDevicePixels="True"
                Foreground="{DynamicResource DynamicFrmFG}"
                Margin="0,5,5,5"
                Content="Download"
                Command="{Binding SelectedItem.Content.DownloadCommand, ElementName=filesList}"
                MinWidth="50" 
                >
                <!--Command="{Binding SelectedItem.Content.DownloadCommand, Source={StaticResource ResourceKey=filesListTemplate}, ElementName=filesList}"-->
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="{DynamicResource DynamicFrmBGColor}" Offset="0"/>
                        <GradientStop Color="{DynamicResource DynamicFrmFGColor}" Offset="2"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
        </StackPanel>
    </Grid>

Code snippet for the DataTemplate (The ListView datasource is set to an ObservableCollection and the FilesViewModel contains a Download command):

<DataTemplate x:Key="filesListTemplate">
    <ListView
        x:Name="filesList"
        SnapsToDevicePixels="True"
        IsTextSearchEnabled="False"
        ItemsSource="{Binding }" 
        HorizontalContentAlignment="Stretch"
        BorderBrush="{DynamicResource DynamicFrmFG}" Foreground="{DynamicResource DynamicFrmFG}" Background="{DynamicResource DynamicFrmBG}"
        ItemContainerStyle="{DynamicResource ListViewItemStyle_Gray}"
        >
        <ListView.View>
            <GridView 
                    AllowsColumnReorder="False"
                    ColumnHeaderContainerStyle="{DynamicResource SpecialityHeader}"
                    >

                <GridViewColumn
                        Width="200" Header="ID">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock 
                                    SnapsToDevicePixels="True"
                                    FontFamily="Consolas" FontSize="14"
                                    Text="{Binding ID}" 
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn
                        Width="200" Header="File Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock 
                                    SnapsToDevicePixels="True"
                                    FontFamily="Consolas" FontSize="14"
                                    Text="{Binding Filename}" 
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn
                        Width="250" Header="Uploaded By">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock 
                                    SnapsToDevicePixels="True"
                                    FontFamily="Consolas" FontSize="14"
                                    Text="{Binding Role}" 
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn
                        Width="200" Header="Uploaded When">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock 
                                    SnapsToDevicePixels="True"
                                    FontFamily="Consolas" FontSize="14"
                                    Text="{Binding UploadedWhen}" 
                                    />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</DataTemplate>

What I am trying to do is this; Bind the button in my control template to the selected FileViewModel, from the ListView, download command. This command, however, is located up in a DataTemplate. Can someone please assist me in doing this successfully?

Edit at 1705EST: Added the View that calls the control template.

0

There are 0 answers