【问题标题】:how to make binding between a list to listbox in xaml?如何在 xaml 中的列表与列表框之间进行绑定?
【发布时间】:2016-03-31 04:51:27
【问题描述】:

我有一个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;
    }

xml 代码:

<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>

【问题讨论】:

  • 现在您只在 XAML 中而不是在代码中进行绑定。你也可以在 XAML 中设置DataContext
  • @AnjumSKhan 那么我应该写什么 DataContext="{Binding ...}"?
  • &lt;Window.DataContext&gt; &lt;Binding RelativeSource="{RelativeSource Self}"/&gt;&lt;/Window.DataContext&gt; ,并在您的列表框中 ItemsSource="{Binding ListHelmets}"

标签: wpf list xaml binding listbox


【解决方案1】:

如果您不使用 MVVM,请在后面的代码中执行此操作:

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

文本块文本 protection 必须是类 ProtectionBase

中的属性

在 xaml 中执行以下操作: 删除 ItemsSource="{Binding}"
从堆栈面板中删除数据上下文,
移除 ,ElementName=ListHelmets[0]

如果您使用的是 MVVM,请告诉我

【讨论】:

  • 我需要 mvvm 吗?我想用头盔表单 ListHelmets 填充列表框,每个头盔都有自己的属性,我想在下面的 texbox 中显示它,事件 - PreviewMouseLeftButtonDown。现在我已经为每个列表框项编写了它,像这样 在括号中使用不同的 [i],猜猜这是不对的@rohHit
  • 你不需要 MVVM。照我之前说的去做。
【解决方案2】:

这肯定会奏效:

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 中删除了所有事件和不必要的东西。请添加所有必要的事件)

我不明白需要这个文本块:

&lt;TextBlock FontWeight="Bold" FontSize="13" Background="Silver" Margin="0 0 0 10" Text="Шлемы"&gt;&lt;/TextBlock&gt;

我认为你应该把它放在列表框上方。你不应该把它放在 ListBox 里面

【讨论】:

    猜你喜欢
    • 2020-04-03
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多