我想開發一個自定義用戶控件。在我的用戶控件中,我使用兩個控件列表框和文本框。文本框用於過濾列表框中的項目。爲此,我在過濾方法中遇到問題。在我的過濾器方法中,我需要將對象投射到ItemSource類型。但我不明白我該如何施展它。這是我的代碼,我嘗試:C#:如何將對象轉換爲ItemSource數據類型?
public static readonly DependencyProperty ItemSourceProperty = DependencyProperty.Register("ItemSourrce", typeof(IEnumerable), typeof(SSSearchListBox), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged)));
private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var control = sender as SSSearchListBox;
if (control != null)
control.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
}
private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
// Remove handler for oldValue.CollectionChanged
var oldValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
if (null != oldValueINotifyCollectionChanged)
{
oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
}
// Add handler for newValue.CollectionChanged (if possible)
var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged;
if (null != newValueINotifyCollectionChanged)
{
newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged);
}
}
void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//Do your stuff here.
}
public IEnumerable ItemSourrce
{
get { return (IEnumerable)this.GetValue(ItemSourceProperty); }
set { this.SetValue(ItemSourceProperty, value); }
}
public void TextFiltering(ICollectionView filteredview, DevExpress.Xpf.Editors.TextEdit textBox)
{
string filterText = "";
filteredview.Filter = delegate(object obj)
{
if (String.IsNullOrEmpty(filterText))
{
return true;
}
string str = obj as string; // Problem is here.
// I need to cast obj to my ItemSourrce Data Type.
if (String.IsNullOrEmpty(obj.ToString()))
{
return true;
}
int index = str.IndexOf(filterText, 0, StringComparison.InvariantCultureIgnoreCase);
return index > -1;
};
textBox.EditValueChanging += delegate
{
filterText = textBox.Text;
filteredview.Refresh();
};
}
private void textEdit1_GotFocus(object sender, RoutedEventArgs e)
{
ICollectionView view = CollectionViewSource.GetDefaultView(ItemSourrce);
TextFiltering(view, textEdit1);
}
調用該使用控制:
List<testClass> myList = new List<testClass>();
public void testMethod()
{
for (int i = 0; i < 20; i++)
{
myList.Add(new testClass { testData=i.ToString()});
}
myTestControl.ItemSourrce = myList;
}
public class testClass
{
public string testData { get; set; }
}
thank`s
要assing到的ItemSource(或ItemsSource時命名的一致性)什麼
是的,我同意你的看法。 但是,如果我把myTestControl1.ItemSourrce = myList1; 和myTestControl2.ItemSourrce = myList2; 那麼會發生什麼?我想用我的控件,如列表框。 – 2012-04-29 06:03:24
@ Hasan009只要兩個ItemsSource都是testClass類型,它就會正常工作。因爲您每次都將過濾器分配給特定的視圖。對於你所有的觀點來說,這不是同一個過濾器。 – Dummy01 2012-04-29 06:10:58
其實我想要得到我的ItemSourrce的類型。在這種情況下,數據類型是testClass.I想要在幾個項目中使用我的控件,因此數據源將發生更改。 – 2012-04-29 07:13:59