如何从列表视图中获取控件? [英] how to get controls from list view?

查看:66
本文介绍了如何从列表视图中获取控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图和两列国家和州(都是组合框)。我想过滤国家组合框的状态组合框.i没有在comboCountry_SelectionChanged事件中获得组合状态的对象。帮助我

i have a listview and two columns Country and state (both are combo box).i want to filter state combo box with country combo box .i didnt get object of combo State in comboCountry_SelectionChanged Event. help me

推荐答案

这可能是最简单的方法,创建一个带有两个 DataTable的 DataSet ,国家和州,两者之间有关系。



然后你使用两个 BindingSource ,一个用于国家,一个用于国家,用作组合框的 DataSource



下面的代码是为Windows窗体编写的,带有两个常规的 ComboBox 控件,但是对于你的情况,校长是相同的。

只有最后一部分分配组合框的代码的数据源应该不同。

This is probably easiest done by creating a DataSet with two DataTable, Countries and States, with a relation in between.

Then you use two BindingSource, one for Countries and one for States, that you use as DataSource for your combo boxes.

The code below is written for a Windows Form with two regular ComboBox controls, but the principal is the same for your case.
Only the last part of the code where the combo boxes are assigned the data sources should differ.
DataSet dsData = new DataSet("CountriesAndStates");

DataTable dtCountries = dsData.Tables.Add("Countries");
DataColumn dcPK = dtCountries.Columns.Add("CountryID", typeof(int));
dtCountries.Columns.Add("Name", typeof(string));

dcPK.AutoIncrement = true;
dtCountries.PrimaryKey = new DataColumn[] { dcPK };

DataTable dtStates = dsData.Tables.Add("States");
dtStates.Columns.Add("Name", typeof(string));
DataColumn dcFK = dtStates.Columns.Add("CountryID", typeof(int));

dsData.Relations.Add("Country_State", dcPK, dcFK);

DataRow drParent = null;

drParent = dtCountries.NewRow();
drParent["Name"] = "USA";
dtCountries.Rows.Add(drParent);

dtStates.Rows.Add("Alabama", drParent["CountryID"]);
dtStates.Rows.Add("Alaska", drParent["CountryID"]);
dtStates.Rows.Add("Utah", drParent["CountryID"]);

drParent = dtCountries.NewRow();
drParent["Name"] = "Canada";
dtCountries.Rows.Add(drParent);

dtStates.Rows.Add("Alberta", drParent["CountryID"]);
dtStates.Rows.Add("British Columbia", drParent["CountryID"]);
dtStates.Rows.Add("Quebec", drParent["CountryID"]);

BindingSource bsCountries = new BindingSource(dsData, "Countries");
BindingSource bsStates = new BindingSource(bsCountries, "Country_State");

cbCountries.DataSource = bsCountries;
cbCountries.DisplayMember = "Name";

cbStates.DataSource = bsStates;
cbStates.DisplayMember = "Name";





我也尝试在WPF中实现这个功能,但作为这项技术的全新手,我发现它有点困难。

In换句话说,我悲惨地失败了。 :-(

今天没有时间了。



I tried to do an implementation of this in WPF as well, but being a total newbie to this technology I found it a bit difficult.
In other words, I failed miserably. :-(
And no more time today.


private System.Windows.DependencyObject FindChildControl<T> ( System.Windows.DependencyObject control , string ctrlName , long lRowid )
      {
          int childNumber = System.Windows.Media.VisualTreeHelper.GetChildrenCount ( control );
          for ( int i = 0 ; i < childNumber ; i++ )
          {
              System.Windows.DependencyObject child = System.Windows.Media.VisualTreeHelper.GetChild ( control , i );
              System.Windows.FrameworkElement fe = child as System.Windows.FrameworkElement;
              // Not a framework element or is null
              if ( fe == null ) return null;

              if ( child is T && fe.Name == ctrlName && fe.Tag.ToString ( ) == lRowid.ToString ( ) )
              {
                  // Found the control so return
                  return child;
              }
              else
              {
                  // Not found it - search children
                  System.Windows.DependencyObject nextLevel = FindChildControl<T> ( child , ctrlName , lRowid );
                  if ( nextLevel != null )
                      return nextLevel;
              }
          }
          return null;
      }


这篇关于如何从列表视图中获取控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆