steve
steve

Reputation: 927

Displaying list from database in ListView that is already binded

To clarify what is going on. Basically I have a ListView binding which points to a List within an object. Within that same object (but not within the list) I have another list which holds strings used for a dropdown and I cannot assign it to my list view as the DataContext is already set to the first list mentioned. Can someone please offer a solution, or better yet a more efficient way to handle this?

View

<ListView ItemsSource="{Binding myModel.myCollection}" Grid.Row="1" Grid.Column="0">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Name, Mode=TwoWay}"></TextBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="Category Tag">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding myModel.CategoryList}"></ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

Model

public class SiteUrlsModel : INotifyPropertyChanged
    {
        public string CaseName { get; set; }
        public List<string> TestList => new List<string> { "Test1", "Test2", "Test3" };

        public List<string> _categoryTagList;
        public List<string> CategoryTagList
        {
            get => _categoryTagList;
            set
            {
                if (_categoryTagList == value)
                    return;
                _categoryTagList = value;
                OnPropertyChanged();
            }
        }

        private ObservableCollection<SiteUrlsModel> _myCollection;
        public ObservableCollection<SiteUrlsModel> myCollection
        {
            get => _siteurlscCollection;
            set
            {
                if (_siteurlscCollection == value)
                    return;
                _siteurlscCollection = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

For simplicity I have excluded the ViewModel and Code-Behind but after InitialiseComponent() I have DataContext = new TestViewModel() and in my ViewModel I have a property which creates a new instance of my Model as well as adding a getter to ensure everything is accessible. Rest assured the list gets populated I am simply trying to populate one dropdown separately.

Upvotes: 0

Views: 22

Answers (1)

Arun Selva Kumar
Arun Selva Kumar

Reputation: 2732

This is happening because, the Combo Box's datacontext will be myModel's item.
You need to explicitly tell the combo box to get the itemssource from it's parent's datacontext.

<DataTemplate>
     <ComboBox ItemsSource="{Binding DataContext.myModel.CategoryList, RelativeSource={RelativeSource AncestorType=DataGrid}}"></ComboBox>
</DataTemplate>

Upvotes: 1

Related Questions