locked
How to get the selected ComboBoxItem in a DataTemplate RRS feed

  • Question

  • Hello every, in my project, I use a DataTemplate:

    <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 >
                        <ComboBoxItem Content="One-way Sync" />
                        <ComboBoxItem Content="Mirror Sync"/>
                        <ComboBoxItem Content="Two-way Sync"/>
                       
                    </ComboBox>
                </StackPanel>
            </Grid>
        </DataTemplate>

    I bind it to an ObservableCollection

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

    GridView1.DataContext = All_FolderPairs_Details;

    Here is the effect:

    Because I want to allow the user to change the sync type for each item that they want, so I need to know which ComboBoxItem the user selected, and the number of the items in the grid view.

    But I don't know how to do it.

    If you know, please tell me.

    Much thanks!!

    Saturday, August 17, 2013 7:00 AM

Answers

  • The combobox has a "SelectedItem" property that'll tell you which one was selected, as well as a "SelectedIndex" property. I suggest that you add a property in your datamodel for each gridview item that does a binding back to the item, so you get it that way. ie something like:
       <ComboBox SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" >

    and add a property to your FolderPair:
        public int SelectedIndex { get; set; } //probably add some get/set code to handle the setter getting hit

    So the trick is really not about getting at the comboxitem, but about getting the data to flow back up into your datamodel. That way you keep your data from knowing about the UI and you save writing some code as well, and no need to maintain that code should you choose to change the UI.


    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net


    Sunday, August 18, 2013 4:59 AM

All replies

  • The combobox has a "SelectedItem" property that'll tell you which one was selected, as well as a "SelectedIndex" property. I suggest that you add a property in your datamodel for each gridview item that does a binding back to the item, so you get it that way. ie something like:
       <ComboBox SelectedIndex="{Binding SelectedIndex, Mode=TwoWay}" >

    and add a property to your FolderPair:
        public int SelectedIndex { get; set; } //probably add some get/set code to handle the setter getting hit

    So the trick is really not about getting at the comboxitem, but about getting the data to flow back up into your datamodel. That way you keep your data from knowing about the UI and you save writing some code as well, and no need to maintain that code should you choose to change the UI.


    /Morten
    twitter: http://www.twitter.com/dotMorten
    blog: http://www.sharpgis.net


    Sunday, August 18, 2013 4:59 AM
  • Thank you, I think you have bring me to the correct direction
    Monday, August 19, 2013 8:03 AM