Note that there are some explanatory texts on larger screens.

plurals
  1. POupdate itemsource in codebehind not changing with xaml binding
    primarykey
    data
    text
    <p>The problem is that I’m changing the collection of items at runtime and I can’t get the combobox to update to show the new items. I’d like to achieve this via xaml.</p> <p>I’d like to solve this for a single combobox, and then also for a datagrid either as either a datagridComboBoxColumn or as a templatecolumn containing a combobox as the datatemplate. </p> <p>I’ve got code like:</p> <pre><code>public class Member { public string PublicID {get; set;} public string Description {get; set;} } public ObservableCollection&lt;Member&gt; ComboBoxSource; public UpdateComboBoxContents() { List&lt;Member&gt; newList; // Omitted Code to retrieve list from datasource.. ComboBoxSource = new ObservableCollection&lt;Member&gt;(newList); // If I uncomment the next line, combobox will show new contents: //myComboBox.itemssource = ComboBoxSource; // I’ve also tried.. OnPropertyChanged("ComboBoxListSource"); } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string Name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(Name)); } } </code></pre> <p>Where:</p> <pre><code>public partial class MyForm: UserControl, INotifyPropertyChanged </code></pre> <p>Combobox xaml looks like:</p> <pre><code>&lt;ComboBox Name="myComboBox" SelectedValuePath="PublicID" DisplayMemberPath="Description" ItemsSource="{Binding ComboBoxListSource, UpdateSourceTrigger=PropertyChanged}"/&gt; </code></pre> <p>I figure I’m screwing up binding or implementing INotifyPropertyChanged. In debug I noticed that the handler is always null so the event isn’t raised.</p> <p>For the second part of this question (implementing into a datagrid) I’ve got:</p> <pre><code>public observableCollection&lt;DatarowWithMember&gt; ListDataRowWithMember; // Code to populate list.. myDataGrid.Itemsource = ListDataRowWithMember </code></pre> <p>where DataRowWithMember is a class that implements INotifyPropertyChanged and has a MemberID property which should point to Member’s PublicID</p> <p>I’ve tried this xaml:</p> <pre><code>&lt;DataGridTemplateColumn Header="Group" Width="*"&gt; &lt;DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;ComboBox ItemsSource="{Binding ComboBoxListSource, RelativeSource={RelativeSource AncestorType=UserControl}}" SelectedValue="{Binding MemberID, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Description" SelectedValuePath="PublicID" IsHitTestVisible="False" SelectionChanged="ComboBoxChanged"&gt; &lt;/ComboBox&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellTemplate&gt; &lt;DataGridTemplateColumn.CellEditingTemplate&gt; &lt;DataTemplate&gt; &lt;ComboBox ItemsSource="{Binding ComboBoxListSource, RelativeSource={RelativeSource AncestorType=UserControl}}" DisplayMemberPath="Description" SelectedValuePath="PublicID" SelectedValue="{Binding MemberID, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="ComboBoxChanged"/&gt; &lt;/DataTemplate&gt; &lt;/DataGridTemplateColumn.CellEditingTemplate&gt; &lt;/DataGridTemplateColumn&gt; </code></pre> <p>Solution: As others pointed out, I had a typo with ComboBoxSource and ComboBoxListSource - this wasn't a problem in code but my error in writing out this question.</p> <p>Checking output window did indeed show binding problems namely that property ComboBoxSource could not be found. I changed to:</p> <pre><code>private ObservableCollection&lt;Member&gt; _ComboBoxSource = new ObservableCollection&lt;Member&gt;() public ObservableCollection&lt;Member&gt; ComboBoxSource { get { return _ComboBoxSource; } } </code></pre> <p>and that worked. class Properties vs members ?</p>
    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.
    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