locked
How can I get the selected item index of each combobox items in a item Template in Gridview RRS feed

  • Question

  • Hi everyone, I am developing a folder sync app.

    Here is the testcase of my codes:

    Source codes of my project

    In my project I have defined a class that store the folder pair information.

    public class FolderPair
            {
                public FolderPair() { }

                public FolderPair(string FolderPairName_Defined_by_User, string SourceFolder_Path, string DestinationFolder_Path, int SycnType)
                {
                    This_FolderPairName_Defined_by_User = FolderPairName_Defined_by_User;
                    This_SourceFolder_Path = SourceFolder_Path;
                    This_DestinationFolder_Path = DestinationFolder_Path;
                    This_SycnType = SycnType;
                }

                public string This_FolderPairName_Defined_by_User { get; set; }
                public string This_SourceFolder_Path { get; set; }
                public string This_DestinationFolder_Path { get; set; }
                public int This_SycnType { get; set; }

                // Override the ToString method.
                public override string ToString()
                {
                    return This_FolderPairName_Defined_by_User;
                }
            }

    Then I defined an ObservableCollection for the folder pairs:

    public ObservableCollection<FolderPair> All_FolderPairs_Details = new ObservableCollection<FolderPair>();

    And I defined a item template for the folderpair items:

                        

    <DataTemplate x:Key="Standard250x250ItemTemplate">
            <Grid HorizontalAlignment="Left" Width="250" Height="250">
                <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
                    <Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
                </Border>
                <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
                    <TextBlock Text="{Binding This_SourceFolder_Path}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="60" Margin="15,0,15,0"/>
                    <TextBlock Text="{Binding Subtitle}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
                    <ComboBox SelectedIndex="{Binding This_SycnType}"  >
                        <ComboBoxItem Content="One-way Sync" />
                        <ComboBoxItem Content="Mirror Sync"/>
                        <ComboBoxItem Content="Two-way Sync"/>

                    </ComboBox>
                </StackPanel>
            </Grid>
        </DataTemplate>

    To test it, I wrote the following codes:

              

      private void Button_Click(object sender, RoutedEventArgs e)
            {
                All_FolderPairs_Details.Add(new FolderPair("FolderPair1", "SourcePath 1", "DestinationPath 1", 0));
                All_FolderPairs_Details.Add(new FolderPair("FolderPair2", "SourcePath 2", "DestinationPath 2", 1));
                All_FolderPairs_Details.Add(new FolderPair("FolderPair3", "SourcePath 3", "DestinationPath 3", 2));
                GridView1.DataContext = All_FolderPairs_Details;


            }

    Here is the effect:

    Here are my questions:

    Question 1:

    Because I want to allow the user to change the sync type whenever they wish, so I need to know which combobox items they selected for each gridview item.

    I understood there is a selected items method, but how can I get the combobox item object in the items. I don't know how.

    Question 2:

    Another question is that for the item template, I have set:

    ComboBox SelectedIndex="{Binding This_SycnType}" 

    I thought becasue I have set the binding, when the user changed the selected item, the This_SycnType should be changed and return the value in the according folder pair. Why it didn't? 

    If you know, please tell me. Thank you very much!



    Monday, April 21, 2014 2:52 AM

Answers

All replies

  • This is too long to take it all in. Please simplify your questions.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, April 21, 2014 1:04 PM
    Moderator
  • Hi, Matt, simply speaking, I bind my folderpair collection to the item template, then when the users change the selected index of each item(see my screen capture), how can I know which combobox item index they selected for each item(I could't find a way to represent the combox objects in the Gridview item).

    Thanks!

    Monday, April 21, 2014 2:21 PM
  • Any idea? Thanks!
    Tuesday, April 22, 2014 2:34 PM
  • See http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh758322.aspx for information on setting up a master/detail binding. If that's not what you are looking for then please describe what you are doing and looking for more simply and clearly.

    --Rob

    • Marked as answer by Delphi Huang Wednesday, April 23, 2014 11:48 AM
    Tuesday, April 22, 2014 2:53 PM
    Moderator
  • Thanks Rob, I think this is the correct way to do it.
    Wednesday, April 23, 2014 11:49 AM