Sebastian Malton Sebastian Malton - 3 months ago 20
C# Question

Bound function parameters UWP

I have a list view in my XAML code for displaying items in a list, I would like to "select" on when I double click on it.

<ListView x:Name="sounds">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" DoubleTapped="select_cue({Binding})">
<TextBlock .../>
<Slider .../>
...
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>


What I would like is the function in my C# code to be called when the StackPanel is "DoubleTapped".

public void select_cue(SoundCue cue) {
//find cue in list of cues
//make current cue point to passed in cue if it is in the list
}


However, when I try and compile this I get "Error: CS1026 ) expected". I have tried to search around for this functionality which I am sure exists (because similar styles of application APIs like AngularJS do have this functionality).

Answer

You need to change your mode of accessing items in a little different way.

<ListView x:Name="sounds">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" DoubleTapped="StackPanel_DoubleTapped">
                <TextBlock .../>
                <Slider .../>
                ...
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And your StackPanel_DoubleTapped Will be

private void StackPanel_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
    //Since you are databinding your sounds, sounds.SelectedItem will be your selected cue.
}
Comments