사용자 지정 사용자 지정 컨트롤을 개발하려고합니다. 내 사용자 정의 컨트롤에서 두 개의 컨트롤을 목록 상자 및 텍스트 상자를 사용합니다. 텍스트 상자는 목록 상자에서 항목을 필터링하는 데 사용됩니다. 이렇게하려면 내 필터 메서드에서 문제가 발생했습니다. 내 필터 메서드에서 개체를 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가 당신이 (일관성 명명 또는 ItemsSource) ItemSource에 assing 싶은
그래 난 당신과 동의합니다. 하지만 만약 내가 넣어 myTestControl1.ItemSourrce = myList1; 그리고 myTestControl2.ItemSourrce = myList2; 그러면 어떻게 될까요? 내 컨트롤을 목록 상자처럼 사용하고 싶습니다. Hasan009 @ –
는만큼 모두 ItemsSource는 타입 인으로는 제대로 작동의 TestClass. 매번 특정보기에 필터를 지정하기 때문입니다. 모든보기에 대해 동일한 필터가 아닙니다. – Dummy01
실제로 나는 ItemSourrce의 유형을 얻고 싶습니다. 이 경우 데이터 유형은 testClass입니다. 여러 컨트롤에서 컨트롤을 사용하고자하므로 데이터 소스가 변경됩니다. –