0

I am trying to link a list of objects in a cs to an XAML file It shows me all the fields of the object even though I wrote that I only want the license number cs:

IBL bl = BLFactory.GetBL("1");
        private ObservableCollection<BO.Bus> BusList = new ObservableCollection<BO.Bus>();//Bus List 
        public BusWindow()
        {
            InitializeComponent();
            BusList=Convert<BO.Bus>(bl.GetAllBus());
            lvBuses.ItemsSource = BusList;
        }
        public ObservableCollection<T> Convert<T>(IEnumerable<T> original)
        {
            return new ObservableCollection<T>(original);
        }
xaml:
<Grid>
        <ListView x:Name="lvBuses" ItemsSource="{Binding AllItems}" DisplayMemberPath="LicensePlate" HorizontalAlignment="Left" Height="271" Margin="271,32,0,0" VerticalAlignment="Top" Width="452">
            <ListView.View>
                <GridView >
                    <GridViewColumn Header="License number"  Width="120"/>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Click="ViewDetailsClick" Content="View Details" Margin="150,1,0,0" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
Naama
  • 7
  • 2
  • I see in your code that the ItemsSource is set up twice: one from code behind (lvBuses.ItemsSources = BusList) and one in the XAML (ItemsSources="{Binding AllItems}". Are you sure you posted the right code? If so, try to remove the binding in XAML and, if the problem persists, try to share the BO.Bus class – PiGi78 Dec 29 '20 at 22:18
  • Maybe try the approach suggested at https://stackoverflow.com/a/12995073/5652483 – Scott Hutchinson Dec 29 '20 at 22:20
  • I removed the binding in the XAML, But it still does not work, I have an override of Tostring function. What happens is that it shows me all the fields of the object I want only one – Naama Dec 29 '20 at 22:28
  • DisplayMemberPath is only effective if you don't set an ItemTemplate or a View with GridViewColumns. – Clemens Dec 29 '20 at 22:38
  • Why do you have it with a `Margin` of "271,32,0,0"? Are you sure you are seeing it? – ΩmegaMan Dec 29 '20 at 22:38
  • The more logical answer is that the `ItemsSource` is not getting any data and the `Cast` is silently failing. – ΩmegaMan Dec 29 '20 at 22:50

0 Answers0