简体   繁体   中英

Listbox databinding NewItemPlaceholder

I have an observable collection bound to a list box. The collection has 2 items, but the list box is showing 3 items (eg the 2 items that are actually in the observable collection and an additional item for the NewItemPlaceholder.

I want it only to show the 2 items.

Below is my XAML.

<ListBox MinHeight="20" MinWidth="20" Name="MultipleSelectionsMultipleWagersListBox"  Visibility="{Binding Path=Coupon.BarcodeText, Converter={StaticResource CouponBarcodeToVisibilityConverter1}, ConverterParameter=994450_994550}" Height="AUto" Width="Auto" VerticalAlignment="Stretch"  HorizontalAlignment="Stretch" Margin="5"
         ItemsSource="{Binding Path=BetViewModels}" Grid.Row="1" Grid.Column="1" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <View:BetView  DataContext="{Binding}" Name="ThisBet" Margin="5"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Here is the c#

  private ObservableCollection<BetViewModel> _betViewModels = new ObservableCollection<BetViewModel>();
    public ObservableCollection<BetViewModel> BetViewModels
    {
        get { return _betViewModels; }
        set
        {
            if (Equals(value, _betViewModels)) return;
            _betViewModels = value;
            OnPropertyChanged("BetViewModels");
        }
    }

Here is the code to populate the betViewModels:

var betViewModel = new BetViewModel { Bet = new Bet() };
betViewModel.Bet.SelectionName = "Chelsea";
betViewModel.Bet.Price = "4/9";
betViewModel.Bet.Market = "90 Minutes";
betViewModel.Bet.ExpectedOdd = DateTime.Now;
BetViewModels.Add(betViewModel);

betViewModel = new BetViewModel { Bet = new Bet() };
betViewModel.Bet.SelectionName = "Chelsea";
betViewModel.Bet.Price = "4/9";
betViewModel.Bet.Market = "90 Minutes";
betViewModel.Bet.ExpectedOdd = DateTime.Now;
BetViewModels.Add(betViewModel);

How Do I switch of this from showing the additional item for the new item place

Here is an image of it displaying the placeholder

在此输入图像描述

There's nothing in your code that should be adding an extra empty item. There may be some other code adding to BetViewModels or there may be a change happening to the generated ICollectionView for the collection if you have it bound to something else that you're not showing, like an editable DataGrid.

The DataGrid supports adding new rows, which have to start out blank. If your ItemsSource is bound to both a ListBox/ItemsControl and a DataGrid, you need to set the DataGrid 'CanUserAddRows' property to 'False'.

Where I found the answer: http://www.mindstick.com/Forum/1519/How%20do%20I%20remove%20a%20listbox%20new%20item%20placeholder

did your sample code also provide this issue?

how much items contains your _betViewModels.count in debugging there are really only 2 Items?

it seems you added an empty BetViewModel at the End

i would suggest check your logic which provides populates your items

  • if it is a loop it should (counter<yourDatasource.Count) just for example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM