2017-02-09 43 views
0
ProjectInformation instance = lstbxindex.SelectedItem as ProjectInformation; 
string name = instance.ProjectRow.Name; 
IEditableCollectionView items = lstbxindex.Items; 
if(items.CanRemove) 
{ 
    items.Remove(lstbxindex.SelectedItem); 
} 

使用這些代碼行刪除listboxitems。編輯後我需要在列表框中添加值。如何在列表框中添加一個新的項目,其中從代碼後面綁定到xaml頁面

XAML

<ListBox ItemsSource="{Binding}" HorizontalContentAlignment="Left" x:Name="lstbxindex" SelectionMode="Extended" Foreground="White" FontSize="20px" Height="241" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <WrapPanel Orientation="Horizontal" Margin="5" > 
       <TextBlock Height="40px" Width="80px" Text="{Binding Roundedhour1}" FontSize="24" Background="#555555" Foreground="Black"></TextBlock> 
       <Label x:Name="items" Content="{Binding ProjectRow.Name}" Margin="35,0,0,0" MouseDoubleClick="items_MouseDoubleClick" Foreground="White"></Label> 
      </WrapPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+1

你應該[收到錯誤](http://stackoverflow.com/q/27348796/1997232)努力時添加項目。你在哪裏設置'DataContext'?數據是什麼類型?它必須是['的ObservableCollection <>'](https://msdn.microsoft.com/en-us/library/ms668604(V = vs.110)的.aspx),然後簡單地用'Add' /'Insert'該集合的方法將項目添加/插入到'ListBox'中。 – Sinatr

+0

的[將項目添加到綁定WPF列表框]可能的複製(http://stackoverflow.com/questions/1307067/adding-an-item-to-a-bound-wpf-listbox) – Sinatr

+0

我不習慣可觀察到的採集。 – user688

回答

1

ListBoxItemsSource屬性設置爲ObservableCollection<ProjectInformation>,並添加和刪除使用AddRemove方法從此Collection項目。

XAML:

<ListBox HorizontalContentAlignment="Left" x:Name="lstbxindex" SelectionMode="Extended" Foreground="White" FontSize="20px" Height="241" BorderBrush="#555555" Margin="10,34,16,0" VerticalAlignment="Top" Width="322" Background="#555555" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <WrapPanel Orientation="Horizontal" Margin="5" > 
       <TextBlock Height="40px" Width="80px" Text="{Binding Roundedhour1}" FontSize="24" Background="#555555" Foreground="Black"></TextBlock> 
       <Label x:Name="items" Content="{Binding ProjectRow.Name}" Margin="35,0,0,0" MouseDoubleClick="items_MouseDoubleClick" Foreground="White"></Label> 
      </WrapPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

代碼:

public class ProjectInformation 
{ 
    public int Roundedhour1 { get; set; } 
} 

public partial class MainWindow : Window 
{ 
    private ObservableCollection<ProjectInformation> _sourceCollection = new ObservableCollection<ProjectInformation>(); 
    public MainWindow() 
    { 
     InitializeComponent(); 
     lstbxindex.ItemsSource = _sourceCollection; 

     //add 
     ProjectInformation item = new ProjectInformation() { Roundedhour1 = 1 }; 
     _sourceCollection.Add(item); 
    } 

    private void items_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
    { 
     //remove 
     _sourceCollection.Remove(lstbxindex.SelectedItem as ProjectInformation); 
    } 
} 
相關問題