【问题标题】:Bind a listbox inside a listbox to an observable collection? Best approach?将列表框中的列表框绑定到可观察的集合?最好的方法?
【发布时间】:2012-06-19 20:56:46
【问题描述】:

在 xaml 中,我有一个列表框,其中有一个 1 行 2 列的网格。在第一列中我有一个名称,在第二列中我有另一个列表框...我将第一个列表框绑定到 Enclosure 项目的可观察集合。在 Enclosure 类中,我有另一个 observablecollection 服务器(另一个类)。我也在尝试将其绑定到列表框。当我将项目添加到可观察集合时,EnclosureID 正在正常工作和更新。但是,我不太确定如何让 Slist 绑定到我在其他列表框中的列表框。谁有想法?我可以使用其他方法吗?

    public class Enclosure
    {
        private string enclosureID; //bound to the first listbox

        //I want to bind this below to the second listbox
        //There is another class called Server with various properties
        public ObservableCollection<Server> Slist = new ObservableCollection<Server>();

        public string EnclosureID
        {
            get { return enclosureID; }
            set { enclosureID = value; }
        }
    }

在 xaml 中:

    <ListBox x:Name="lb1" ItemsSource="{Binding}" DataContext="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>

                    <TextBlock x:Name="txtEnclosure" Text="{Binding Path=EnclosureID}" Background="Aqua" Grid.Column="0" Grid.Row="0" />
                    <ListBox x:Name="lbserver" ItemsSource="{Binding Slist}"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" Grid.Column="1" Grid.Row="0">
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel/>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>

                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock x:Name="txtServer" Text="{Binding Path=HostnameID}" Background="Beige"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

要填充列表,我使用此方法:

            //Query the database for enclosures and populate the Enclosure observable collection in the Settings class
    public void GetEnclosures()
    {
        bool exception = false;
        Enclosure enclosure = new Enclosure();
        Server server = new Server();
        server.HostnameID = "HEY";

        OleDbCommand GetEnclosuresCommand = new OleDbCommand(Settings.GetEnclosuresQuery, Settings.conn);

        Settings.conn.Open();
        try
        {
            Settings.myReader = GetEnclosuresCommand.ExecuteReader();//begin reading
        }
        catch
        {
            MessageBox.Show("The Enclosure table is currently being used by another application. Please close the table and run this application again.");
            Settings.conn.Close();
            exception = true;
        }

        if (!exception)
        {
            // while there are enteries to retrieve
            while (Settings.myReader.Read())
            {
                enclosure.EnclosureID = Settings.myReader.GetString(0);
                enclosure.Slist.Add(server);
                Settings.Elist.Add(enclosure);
                enclosure = new Enclosure();
                server = new Server();
                server.HostnameID = "HI";
            }

            // Close when done reading.
            Settings.myReader.Close();

            // Close the connection.
            Settings.conn.Close();
        }
        else
        {
            this.NavigationService.Navigate(new Uri("MainPage.xaml", UriKind.Relative));
        }

    }

【问题讨论】:

  • 我想我知道出了什么问题,但是您能否也发布调用 GetEnclosures 并设置 UI 的 DataContext 的代码?
  • 其实我觉得我知道问题了,把Slist改成属性,加个getter和setter就行了,不然绑定不了UI

标签: c# wpf xaml binding listbox


【解决方案1】:

可能是错的,但不像...那么简单

<ListBox 
  x:Name="lbserver" 
  ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
  Grid.Column="1" 
  Grid.Row="0" 
  ItemsSource="{Binding Slist}">

也改一下

public ObservableCollection<Server> Slist = new ObservableCollection<Server>();

说

public ObservableCollection<Server> Slist { get; set; }
//...
Slist = new ObservableCollection<Server>();

【讨论】:

  • 那可能需要查看更多代码,Slist 是如何填充的?
  • 我将样本数据放入其中,以使用填充其他集合的相同方法对其进行测试:
  • 效果很好,你太棒了:D!!!!!!!这让我困惑了好几天!!!!!!!谢谢你我的朋友!!!
猜你喜欢
  • 2011-02-16
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多