Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hello all,

I synchronize a combobox with an outer listview Element with a DataTable and a column named "Status", which is working: combobox -> listview.

Now I need to write the DataTable value "Status" into the combobox, which I cannot accomplish:
combobox <- listview.

This is needed for example, when returning to the Window with the combobox, after showing a different window. The ListView has always a DataTable with the right "Status" set.

One question I have now is: how can I access ComboBox "comboBoxStatus" from Code Behind? It should be visible, but I cannot see it with code completion. ListView "listView1" on the other hand is accessible. How come?

Thanks in advance
leder

I have combined listview with combobox that way in XAML:



XML
<Grid Background="LightSteelBlue">
<Grid Background="LightSteelBlue">


<ListView Name="listView1" ItemsSource="{Binding}" Background="LightSteelBlue" Visibility="Visible" FontSize="14" IsEnabled="True" Margin="227,67,20,72" IsTextSearchEnabled="True" IsHitTestVisible="True" IsSynchronizedWithCurrentItem="false">
               <ListView.View>
                   <GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
                       <GridViewColumn Width="200" DisplayMemberBinding="{Binding Path=Name}" />
                       <GridViewColumn Width="20" DisplayMemberBinding="{Binding Path=Status}" />
                       <GridViewColumn Width="200">
                           <GridViewColumn.CellTemplate>
                               <DataTemplate>
                                   <ComboBox x:Name="comboBoxStatus"
                                     Style="{DynamicResource ButtonlessComboBoxStyle}"
                                     Width="180" Height="Auto" BorderThickness="0" Background="LightSteelBlue"
                                     ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
                                     SelectedItem="{Binding Path=OperationState, Mode=TwoWay}"
                                     SelectionChanged="comboBox_SelectionChanged"
                                     HorizontalAlignment="Right" VerticalAlignment="Center" HorizontalContentAlignment="Center" IsSynchronizedWithCurrentItem="False" IsReadOnly="False"
                                     >
                                       <ComboBox.ItemTemplate>
                                           <DataTemplate>
                                               <Label  Content="{Binding   Path=.,Mode=OneWay,
                                              Converter={StaticResource enumItemsConverter}}"
                                              Height="Auto"
                                              Margin="0"
                                              VerticalAlignment="Center"/>
                                           </DataTemplate>
                                       </ComboBox.ItemTemplate>
                                   </ComboBox>
                               </DataTemplate>

                           </GridViewColumn.CellTemplate>
                       </GridViewColumn>
                   </GridView>
               </ListView.View>
           </ListView>

</Grid>
      <Border Height="61" HorizontalAlignment="Left" Name="border1" VerticalAlignment="Top" Width="191" CornerRadius="0,0,20,0" BorderThickness="1,2" Opacity="2" Padding="0" BorderBrush="Black" Margin="-9,-12,0,0" />
      <Label Height="30" HorizontalAlignment="Left" Margin="20,11,0,0" Foreground="SlateGray" FontSize="14" FontStyle="Italic" FontWeight="Bold" Name="label2" VerticalAlignment="Top" Width="212">First Boot Assistent</Label>
      <Label Height="29" Margin="208,12,46,0" Foreground="SlateGray" FontSize="14" FontStyle="Italic" FontWeight="Bold" Name="label1" VerticalAlignment="Top">System - Recovery</Label>
      <Label Margin="20,66.845,0,72" Name="label3" HorizontalAlignment="Left" Width="184" BorderThickness="1" Background="Transparent" BorderBrush="Bisque">Hier einführender Text ...</Label>
      <Button Height="33" HorizontalAlignment="Right" Margin="0,0,20,17" Name="button1" VerticalAlignment="Bottom" Width="70" Click="button1_Click">Start</Button>
      <Button Height="33" HorizontalAlignment="Left" Margin="20,0,0,17" Name="button2" VerticalAlignment="Bottom" Width="70" Click="button2_Click">Zurück</Button>
      <Button Height="33" Margin="183,0,176,17" Name="button3" VerticalAlignment="Bottom" Click="button3_Click">Konfiguration komplett löschen</Button>
</Grid>



C# Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using System.Data;
using FirstBootAssistent.Logic;
using FirstBootAssistent.Utility;
using FirstBootAssistent.ValueConverters;

using FirstBootAssistent.Resources;

namespace FirstBootAssistent.GUI
{
    /// <summary>
    /// Interaktionslogik für RecoveryConfig.xaml
    /// </summary>
    public partial class RecoveryConfig : Window
    {
        private FBAservice fbaService;        

        public RecoveryConfig(FBAservice service)
        {
            InitializeComponent();
            fbaService = service;
            
            // ListView mit Daten aus der Tabelle füllen
            listView1.DataContext = fbaService.Tabele();

            //((ComboBox)this.listView1.Items.GetItemAt(0)).ItemsSource = (IEnumerable<int>)fbaService.Tabele();
        }

        // Starten
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // Warnung vor der Ausführung anzeigen
            Window recoveryWarning = new RecoveryWarning(fbaService);
            this.Close();
            recoveryWarning.ShowDialog();            
        }

        // Zurück zur Startmaske
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            // Maske für Recovery-Start anzeigen
            Window recoveryStart = new RecoveryStart(fbaService);
            this.Close();
            recoveryStart.ShowDialog();
        }

        // Konfiguration komplett löschen
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            // Alle Konfigurationsoptionen auf löschen einstellen
            if (!fbaService.ResetAllConfigurationData())
            {
                MessageBox.Show("Einige Daten konten nicht komplett gelöscht werden!", "FBA-Konfiguration",MessageBoxButton.OK, MessageBoxImage.Warning);
            }

            // Maske für abgeschlossen Prozess anzeigen
            Window recoveryFinish = new RecoveryFinish();
            this.Close();
            recoveryFinish.ShowDialog();                
        }

        private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                DependencyObject dep = (DependencyObject)e.OriginalSource;
                while ((dep != null) && !(dep is ListViewItem))
                    dep = VisualTreeHelper.GetParent(dep);
                if (dep == null)
                    return;

                DataRowView item = (DataRowView)listView1.ItemContainerGenerator.ItemFromContainer(dep);
                DataRow dr = fbaService.Tabele().Rows.Find((int)item[FirstBootAssistent.Utility.DataService.TABLE_COLUMN_ID]);
                OperationState myAuswahl = (OperationState)e.AddedItems[0];

                Console.Write("Anwendung: " + dr[FirstBootAssistent.Utility.DataService.TABLE_COLUMN_ID]);
                Console.WriteLine("\tStatus: " + myAuswahl);

                fbaService.SetConfigData((int)dr[FirstBootAssistent.Utility.DataService.TABLE_COLUMN_ID], (int)myAuswahl);
                listView1.DataContext = fbaService.Tabele();

               
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
        }
    }
}
Posted
Updated 12-May-11 23:35pm
v4
Comments
BobJanova 13-May-11 5:35am    
Edited to add WPF tag.

The short answer is: don't, use data binding and have the combo's SelectedIndex property bound to something which gets updated along with the list box. In your case that appears to be 'OperationState': wherever you are updating Name and Status, also fire a PropertyChanged event on OperationState.

The combo is not visible in global scope because it is not one combo, it is a combo within a data template so it is replicated.
 
Share this answer
 
Thanks Bob!

The only change I made to the XAML of the combobox is:
SelectedIndex="{Binding Path=Status}"


And it works like a charm...

Bye
leder
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900