locked
ComboBox in ListView RRS feed

  • Question

  • I have a ComboBox inside a ListView which is working fine with some basic StaticResource databinding.

    I want to change the contents of the ComboBox when it populates though, based on the value of another column in the ListView for the current row.

    How can inspect the value before databinding the ComboBox?

    Thanks in advance.


    Adam
    Monday, April 27, 2009 11:23 AM

Answers

  • Hi

    Here is one similar issue and I posted one demo Apps about that, Please check it

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3ef92d19-0148-4499-9093-d7adf47858db

    Hope this gives you some idea about your binding.
    Yiling, MVP(Visual C++)
    Monday, April 27, 2009 11:39 AM
  • You might want to use a Binding with a Converter that returns your ItemsSource.

            <ListView>
                <ListView.Resources>
                    <cvtrs:ComboBoxItemsSourceConverter x:Key="ComboBoxItemsSourceConverter"/>
                </ListView.Resources>
                <ListView.View>
                    <GridView>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding Path=OtherItemValue, Converter={StaticResource ComboBoxItemsSourceConverter}}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Path=OtherItemValue}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        public class ComboBoxItemsSourceConverter : IValueConverter
        {
            #region IValueConverter Members
    
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                int otherItemValue = (int)value;
    
                if (otherItemValue == 1)
                {
                    //return one set of itemssource
                }
                else
                {
                    //return other set of ItemsSource
                }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    This is not exact since i cant see your code and XAML, but the idea is that you actually bind the ItemsSource of your combo box to the other value directly, and have the converter determine which collection to return as your itemssource, when the other value changes this binding will update
    If this was helpful, please mark as answered
    Blog: AttachedWPF
    AttachedWPF
    Monday, April 27, 2009 8:16 PM

All replies

  • Hi

    Here is one similar issue and I posted one demo Apps about that, Please check it

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3ef92d19-0148-4499-9093-d7adf47858db

    Hope this gives you some idea about your binding.
    Yiling, MVP(Visual C++)
    Monday, April 27, 2009 11:39 AM
  • You might want to use a Binding with a Converter that returns your ItemsSource.

            <ListView>
                <ListView.Resources>
                    <cvtrs:ComboBoxItemsSourceConverter x:Key="ComboBoxItemsSourceConverter"/>
                </ListView.Resources>
                <ListView.View>
                    <GridView>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding Path=OtherItemValue, Converter={StaticResource ComboBoxItemsSourceConverter}}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBox Text="{Binding Path=OtherItemValue}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        public class ComboBoxItemsSourceConverter : IValueConverter
        {
            #region IValueConverter Members
    
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                int otherItemValue = (int)value;
    
                if (otherItemValue == 1)
                {
                    //return one set of itemssource
                }
                else
                {
                    //return other set of ItemsSource
                }
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    This is not exact since i cant see your code and XAML, but the idea is that you actually bind the ItemsSource of your combo box to the other value directly, and have the converter determine which collection to return as your itemssource, when the other value changes this binding will update
    If this was helpful, please mark as answered
    Blog: AttachedWPF
    AttachedWPF
    Monday, April 27, 2009 8:16 PM