locked
Buttons inside DataGrid

    Question

  • Hello,

    I have a ListView control in my Win 8.1 App that is bound to data in a SQLite database (using SQLite-net). Everything is working fine with the binding but I am running into one issue. In each row of the ListView there are three Buttons. If the user clicks a button while that row is selected then everything is fine. But if the user clicks the button while another row is selected then it'll update the other row instead of the one that the button was in. Any idea how I can fix this?

    Thank you,

    nol_b



    • Edited by nol_b Sunday, September 7, 2014 2:22 AM
    Sunday, September 7, 2014 2:06 AM

All replies

  • Sounds like the code in the button event handler uses the selectedIndex property of your grid. It should point to the index of the row it is in. But code should be provided to get an answer to this issue.
    Sunday, September 7, 2014 7:51 AM
  • I will try to put together example code for what I am doing.

    If i understand what you're saying correctly, then yes, I need the code to point to the index of the row that the control is in but I don't know how to get that index. The only thing I can think of is using the GetHasCode for the control and looping through all the rows until the control is found and grabbing the index that way. But even then, I can't figure out how to reference the controls; I can only seem to loop through the items that the list is bound to.

    Sunday, September 7, 2014 11:05 PM
  • My mockup is below. Here's the issue: if the button is in the selected row, then clicking the button will do anything I need it to with the Item bound to that row. If I click a button in a row OTHER THAN the selected row, then I can still only do things with the Item in the selected row. I need to be able to reference rows that aren't selected if the user clicks a button in that row instead of the selected row.

    MainPage.xaml:

    <Page
        x:Class="ProblemExample.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:ProblemExample"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel Orientation="Vertical">
                <TextBlock x:Name="ResultText" Text="Result Text" />
                <ListView x:Name="ProblemList">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition />                            
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <TextBlock x:Name="BoundText1" Grid.Row="0" Grid.Column="0" Text="{Binding Data1}" />
                                <TextBlock x:Name="BoundText2" Grid.Row="0" Grid.Column="1" Text="{Binding Data2}" />
                                <Button x:Name="ProblemButton" Grid.Row="0" Grid.Column="2" Content="Problem" Click="ProblemButton_Click" />
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackPanel>
        </Grid>
    </Page>

    MainPage.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace ProblemExample
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
                List<SampleData> sample = new List<SampleData>();
                
                for (int i = 1; i <=6; i++)
                {
                    string dat1 = "data1" + i.ToString();
                    string dat2 = "data2" + i.ToString();
                    sample.Add(new SampleData()
                    {
                        Data1 = dat1,
                        Data2 = dat2
                    });
                }
    
                ProblemList.ItemsSource = sample;
            }
    
            private void ProblemButton_Click(object sender, RoutedEventArgs e)
            {
                string selectedInd = ProblemList.SelectedIndex.ToString();
                //string controlInd = (Button)sender.something? <---this is my problem
                ResultText.Text = "Index of selected row is " + selectedInd;
            }
        }
    
        public class SampleData
        {
            public string Data1 { get; set; }
            public string Data2 { get; set; }
        }
    }

    Monday, September 8, 2014 1:20 AM
  • Does the code I provided help at all? How do I find the index of the row the control is in?
    Wednesday, September 10, 2014 5:51 AM
  • In case anyone runs into this same issue...my workaround was to use a TextBlock like a Button and use the Tapped event instead of Click. So far this is the only way I can make my app function the way I need it to.
    Thursday, September 11, 2014 3:16 AM
  • Soory for the delay... i was unable to visist this site for some time (ISP Problems).

    I guess to make this with a Button, you could just store the ID of the Element you want to work with in the CommandParameter for the Button.

    <Button x:Name="ProblemButton" Grid.Row="0" Grid.Column="2" Content="Problem" CommandParameter="{Binding Path=IdPropertyOfTheObjbectRepresentedByTheRow}" Click="ProblemButton_Click" />

    If this does not work for some Reason (like there is no ID on that ObjectRepresentedByTheRow) you could try to do this with a FrameworkElement like:

    var temp = ((FrameworkElement)sender).DataContext as ObjectTypeRepresentedByTheRow

    but i have never used the latter one myself

    Tuesday, October 7, 2014 10:24 AM