簡體   English   中英

如何使列表綁定到XAML中的列表框?

[英]how to make binding between a list to listbox in xaml?

我有一個ObservableCollection和一個ListBox ,我想在它們之間進行綁定,但是沒有在后面的代碼中使用listBox1.DataSource/ItemsSource = ... ,我發現了很多這樣的示例,但是我只需要在xaml代碼中進行綁定如果我沒記錯的話,我首先需要將我的清單列為財產。

    private ObservableCollection<ProtectionBase> _listhelmets;
    public ObservableCollection<ProtectionBase> ListHelmets
    {
        get { return _listhelmets; }
        set { _listhelmets = value; }
    } 

    public MainWindow()
    {

        InitializeComponent();
        ListHelmets.Add(new Helmet(){protection=5});
        ListHelmets.Add(new Helmet() { protection = 7 });

        this.DataContext = _listhelmets;
    }

XAML代碼:

<ListBox x:Name="listhelmets" Height="215" Width="197"       PreviewMouseDown="helmet_MouseDown1"
                     PreviewMouseLeftButtonDown="helmet_PreviewMouseLeftButtonDown"
                     PreviewMouseMove="helmet_PreviewMouseMove" ItemsSource="{Binding}">
                <TextBlock FontWeight="Bold" FontSize="13" Background="Silver" Margin="0 0 0 10" Text="Шлемы"> </TextBlock>
                <ListBoxItem >
                    <StackPanel Orientation="Horizontal"  DataContext="{Binding ElementName=listhelmets, Path=SelectedItem}"
                         MouseEnter="ChoosingHelmet1"
                         DragOver="helmet1_DragOver"                                
                         DragEnter="helmet_DragEnter" AllowDrop ="true"
                         DragLeave="helmet_DragLeave" MouseLeftButtonDown="helmet_MouseLeftButtonDown_1">
                       <TextBlock Text="{Binding protection, ElementName=ListHelmets[0]}" Width="124"/>
                    </StackPanel>
                </ListBoxItem>
                <ListBoxItem >
                    <StackPanel Orientation="Horizontal">
                         <Image Source="D:..."/>
                    </StackPanel>
                </ListBoxItem>
</ListBox>

如果您不使用MVVM,請在后面的代碼中執行此操作:

ListHelmets.Add(new ProtectionBase(){protection = 5});
ListHelmets.Add(new ProtectionBase() { protection = 7 });
listhelmets.ItemsSource = ListHelmets;

文本塊文本保護必須是ProtectionBase類中的一個屬性

在xaml中執行以下操作:Remove ItemsSource =“ {Binding}”
從堆棧面板中刪除數據上下文,
刪除,ElementName = ListHelmets [0]

如果您使用的是MVVM,請告訴我

最簡單的方法是使用IObservableCollection

在Xaml上。

<ComboBox  Text="Content File.." 
                       IsEditable="True" 
                       ItemsSource="{Binding MySweetCollection}" 
                       HorizontalAlignment="Right"  
                       />

在背后的代碼中,您必須有一個名為“ MySweetCollection”的IObservableCollection。

在您的代碼中,綁定為空。

這肯定會起作用:

xaml:

  <ListBox x:Name="listhelmets" Height="215" Width="197" ItemsSource="{Binding}"> <TextBlock FontWeight="Bold" FontSize="13" Background="Silver" Margin="0 0 0 10" Text="Шлемы"></TextBlock> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding protection}" Width="124"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

xaml.cs

listhelmets.Items.Clear();
            List<ProtectionBase> Helmets = new List<ProtectionBase>();
            Helmets.Add(new ProtectionBase(){ protection = 7});
            Helmets.Add(new ProtectionBase() { protection = 8 });
            Helmets.Add(new ProtectionBase() { protection = 9 });

            listhelmets.ItemsSource = Helmets;

(我已經從xaml中刪除了所有事件和不必要的東西。請添加所有必要的事件)

我不了解此textblock的需要:

 <TextBlock FontWeight="Bold" FontSize="13" Background="Silver" Margin="0 0 0 10" Text="Шлемы"></TextBlock> 

我認為您應該將其放在列表框上方。 您不應將其放在ListBox中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM