2012-04-29 2 views
0

사용자 지정 사용자 지정 컨트롤을 개발하려고합니다. 내 사용자 정의 컨트롤에서 두 개의 컨트롤을 목록 상자 및 텍스트 상자를 사용합니다. 텍스트 상자는 목록 상자에서 항목을 필터링하는 데 사용됩니다. 이렇게하려면 내 필터 메서드에서 문제가 발생했습니다. 내 필터 메서드에서 개체를 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 싶은

답변

0

아니라 모든 IEnumerable 인터페이스를 구현해야합니다. Thist는 foreach를 통해 반복 할 수있는 기본적인 작업을 의미합니다 (간단히 말하면). 그래서 모든리스트, 배열, 콜렉션, 사전 등등.

은 Normaly 당신은 상자 문자열에서 정상 throu 반복 할 수 없습니다! 개체는 문자열 (디버깅에 중단 점으로 확인) 어떻게 든 그것을 분할해야 정말입니다 그래서 경우

.

PS :는 obj.toString()가 결코 null 또는 비어 있기 때문에 (명시 적 ToStirng()에 대해 빈 문자열을 반환 드문 unmeaningfull 예를 제외하고) 그런데이 코드

if (String.IsNullOrEmpty(obj.ToString())) 
{ 
    return true; 
} 

결코는 true를 반환 . Object. (모든 .net Type이 기반으로 함) 유형의 표준 구현에서 Namespace와 유형 이름을 반환합니다.

0

는 나는 당신의 객체 타입 testClass으로하고 있지 string 잘 이해합니다. 당신이 캐스팅을 수행하려고 할 경우

그래서 코드가 있어야한다.

string str = string.Empty; 

testClass myObject = obj as testClass; 

if (myObject == null) 
    return true; 
else 
    str = myObject.testData; 

if (string.IsNullOrEmpty(str)) 
    return true; 

return str.IndexOf(filterText, 0, StringComparison.InvariantCultureIgnoreCase) > -1; 
+0

그래 난 당신과 동의합니다. 하지만 만약 내가 넣어 myTestControl1.ItemSourrce = myList1; 그리고 myTestControl2.ItemSourrce = myList2; 그러면 어떻게 될까요? 내 컨트롤을 목록 상자처럼 사용하고 싶습니다. Hasan009 @ –

+0

는만큼 모두 ItemsSource는 타입 인으로는 제대로 작동의 TestClass. 매번 특정보기에 필터를 지정하기 때문입니다. 모든보기에 대해 동일한 필터가 아닙니다. – Dummy01

+0

실제로 나는 ItemSourrce의 유형을 얻고 싶습니다. 이 경우 데이터 유형은 testClass입니다. 여러 컨트롤에서 컨트롤을 사용하고자하므로 데이터 소스가 변경됩니다. –

관련 문제