【问题标题】:problems with bindings in listbox with collection带有集合的列表框中的绑定问题
【发布时间】:2014-04-29 18:52:14
【问题描述】:

我有一个带有 6 个列表框的窗口,我正在尝试制作一个小型汽车数据库,但我在第二个列表框中的绑定存在问题。当我尝试单击第一个列表框中的制造商时,绑定不起作用。

<Window x:Class="Autolab.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="500" Width="700"
    Loaded="Window_Loaded_1">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="119*" />
        <ColumnDefinition Width="116*" />
        <ColumnDefinition Width="137*" />
        <ColumnDefinition Width="137*" />
        <ColumnDefinition Width="134*" />
        <ColumnDefinition Width="134*" />
    </Grid.ColumnDefinitions>

    <ListBox x:Name="hersteller" Grid.Column="0" DisplayMemberPath="h_name" SelectedValuePath="h_id"/>

    <ListBox x:Name="marke" Grid.Column="1" 
              ItemsSource="{Binding SelectedItem.marken, ElementName=hersteller}" />

    <ListBox x:Name="kraftstoff" Grid.Column="2" />
    <ListBox x:Name="art" Grid.Column="3" />
    <ListBox x:Name="werkstatt" Grid.Column="4" />


</Grid>

这是我的收藏

public partial class herstellers
{
    public herstellers()
    {
        this.marken = new HashSet<marke>();
    }

    public int h_id { get; set; }
    public string h_name { get; set; }

    public virtual ICollection<marke> marken { get; set; }
}

}

【问题讨论】:

  • 您能否展示您的模型marke 具有属性marken
  • 具有属性标记的模型?我没有将它作为属性外观我发布了我的模型
  • 好吧,我认为如果你用MVVM 做你的事情会容易得多。您不想将来将您的Model 绑定到View
  • 你把东西放在哪里了?
  • 这就是我的信息在标记表中的要点

标签: c# wpf collections binding listbox


【解决方案1】:

首先,我建议使用ObservableCollection 而不是HashSetListBox

public partial class herstellers
{
    public herstellers()
    {
        this.marken = new ObservableCollection<marke>();
    }

    public int h_id { get; set; }
    public string h_name { get; set; }

    public virtual ICollection<marke> marken { get; set; }
}

接下来,假设您的 marken 类,您必须至少有一个实现 ICollection 的属性,以便可以填充第二个列表框上的 ItemsSource

public class Marken
{
  // Assuming this marke is populated.
  public ObservableCollection<int> marke { get; set; }
}

【讨论】:

  • 但我想从我的数据库中获取我的信息
  • @user3371521 将其包裹在ViewModel
  • 你到底是什么意思?我只想通过集合从我的数据库中获取我的绑定
  • @user3371521 检查MVVM 模式。除此之外,请确保您的 marken 是与 marke 相同的集合
猜你喜欢
  • 2012-12-16
  • 2014-07-30
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 2012-05-10
相关资源
最近更新 更多