Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here a simple example using the MVVM Pattern</p> <h2>XAML</h2> <pre><code>&lt;Window x:Class="Binding_a_List_to_a_ComboBox.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid HorizontalAlignment="Left" VerticalAlignment="Top"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="150"/&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="50"/&gt; &lt;RowDefinition Height="25"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;ComboBox Grid.Column="0" Grid.Row="0" ItemsSource="{Binding SearchPointCollection , UpdateSourceTrigger=PropertyChanged}" SelectedIndex="{Binding MySelectedIndex, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding MySelectedItem, UpdateSourceTrigger=PropertyChanged}"&gt; &lt;ComboBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition/&gt; &lt;RowDefinition/&gt; &lt;RowDefinition/&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Text="{Binding Id}" Grid.Row="0"/&gt; &lt;TextBlock Text="{Binding Name}" Grid.Row="1"/&gt; &lt;TextBlock Text="{Binding Otherstuff}" Grid.Row="2"/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ComboBox.ItemTemplate&gt; &lt;/ComboBox&gt; &lt;Button Content="Bind NOW" Grid.Column="0" Grid.Row="1" Click="Button_Click"/&gt; &lt;TextBlock Text="{Binding MySelectedIndex, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="0"/&gt; &lt;Grid Grid.Column="1" Grid.Row="1" DataContext="{Binding MySelectedItem, UpdateSourceTrigger=PropertyChanged}"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition/&gt; &lt;ColumnDefinition/&gt; &lt;ColumnDefinition/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;TextBlock Text="{Binding Id}" Grid.Column="0"/&gt; &lt;TextBlock Text="{Binding Name}" Grid.Column="1"/&gt; &lt;TextBlock Text="{Binding SomeValue}" Grid.Column="2"/&gt; &lt;/Grid&gt; &lt;/Grid&gt; </code></pre> <p></p> <h2>Code</h2> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows; using Import_Rates_Manager; namespace Binding_a_List_to_a_ComboBox { /// &lt;summary&gt; /// Interaktionslogik für MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { DataContext = new coImportReader(); } } } namespace Import_Rates_Manager { public class coImportReader : INotifyPropertyChanged { private List&lt;coSearchPoint&gt; myItemsSource; private int mySelectedIndex; private coSearchPoint mySelectedItem; public List&lt;coSearchPoint&gt; SearchPointCollection { get { return myItemsSource; } set { myItemsSource = value; OnPropertyChanged("SearchPointCollection "); } } public int MySelectedIndex { get { return mySelectedIndex; } set { mySelectedIndex = value; OnPropertyChanged("MySelectedIndex"); } } public coSearchPoint MySelectedItem { get { return mySelectedItem; } set { mySelectedItem = value; OnPropertyChanged("MySelectedItem"); } } #region cTor public coImportReader() { myItemsSource = new List&lt;coSearchPoint&gt;(); myItemsSource.Add(new coSearchPoint { Name = "Name1" }); myItemsSource.Add(new coSearchPoint { Name = "Name2" }); myItemsSource.Add(new coSearchPoint { Name = "Name3" }); myItemsSource.Add(new coSearchPoint { Name = "Name4" }); myItemsSource.Add(new coSearchPoint { Name = "Name5" }); } #endregion #region INotifyPropertyChanged Member public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } #endregion } public class coSearchPoint { public Guid Id { get; set; } public String Name { get; set; } public IRange FoundCell { get; set; } public coSearchPoint() { Name = ""; Id = Guid.NewGuid(); FoundCell = null; } } public interface IRange { string SomeValue { get; } } } </code></pre> <p>Here are 3 Classes:</p> <ul> <li><code>MainWindow</code> which set VM as his Datacontext</li> <li><code>coImportReader</code> the Class which presents your properties for your bindings</li> <li><code>coSearchPoint</code> which is just a Container for your information</li> <li><code>IRange</code> which is just an Interface</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload