2012-06-18 8 views
0

사용 가능한 컬렉션 내의 모든 데이터베이스에 대한 확인란을 제공하는 다음 ItemsControl이 있습니다. 이 확인란을 사용하면 필터링 할 항목을 선택할 수 있습니다. 필터링 할 데이터베이스는 별도의 컬렉션 (FilteredDatabases)에 있습니다. 내가 정확히 이것을 어떻게합니까? InFilter 속성을 데이터베이스 항목 클래스에 추가 할 수 있습니다. 그러나 나는이 코드를 아직 변경하고 싶지 않다. 내 머리 속에서 해결할 수없는 문제는 데이터베이스 항목 자체에없는 속성에 바인딩해야한다는 사실입니다. 어떤 아이디어? 라우팅 명령 인스턴스에ItemsControl 내에서 CheckBox의 IsChecked 속성을 바인딩하는 방법은 무엇입니까?

<ItemsControl ItemsSource="{Binding AvailableDatabases}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
     <CheckBox Content="{Binding Name}" IsChecked="{Binding ???}"/> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

// In view model 

public IBindingList FilteredDatabases 
{ 
    get; 
    private set; 
} 

public IBindingList AvailableDatabases 
{ 
    get; 
    private set; 
} 
+0

viewmodel에 속성을 추가해야합니다.이 속성의 설정자를 사용하여 FilteredDatabases 컬렉션에서 검사 된 데이터베이스를 추가하거나 제거합니다. – jimmyjambles

+0

문제는 내 항목 컨트롤에서 어떤 항목이 선택되었는지를 모르는 것입니다 . 내 뷰 모델로 어떻게 가져 옵니까? – bsh152s

답변

0
  1. 바인딩 CheckBox.Command는
  2. 바인딩은 다음 코드는 당신이 무엇을 설명하는 방법에
  3. 사용 IBindingList.Add 및 IBindingList.Remove 방법
+0

그게 조금 더있어. 확인란을 선택/선택 취소하면 필터링 된 컬렉션에서 항목이 추가/제거됩니다. 그러나 폼을 처음 표시 할 때 필터링 된 컬렉션의 데이터베이스가 검사되도록 뷰를 초기화하는 방법은 무엇입니까? 다시, 바인딩/MVVM을 사용하여이 작업을 수행하고 싶습니다. – bsh152s

0

을 명령을 라우팅 이 작업을 수행하기 위해서는 Collection 객체 대신 ObservableCollection을 사용하는 것이 더 낫습니다. ItemsControl이 바인딩 된 경우 뷰 모델이 자동으로 UI를 업데이트합니다 추가되고 제거됩니다.

XAML :

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 
    <ItemsControl Grid.Column="0" ItemsSource="{Binding AvailableDatabases}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
    <ItemsControl Grid.Column="1" ItemsSource="{Binding FilteredDatabases}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding Name}" /> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Grid> 

보기 모델 :이 특별한 경우에 필요하지 이후

public class MainViewModel 
{ 
    private ObservableCollection<DBViewModel> _availableDatabases; 
    private ObservableCollection<DBViewModel> _filteredDatabases; 

    public ObservableCollection<DBViewModel> AvailableDatabases 
    { 
     get 
     { 
      if (_availableDatabases == null) 
      { 
       _availableDatabases = new ObservableCollection<DBViewModel>(new List<DBViewModel>() 
        { 
         new DBViewModel(this) { Name = "DB1" , IsChecked = true}, 
         new DBViewModel(this) { Name = "DB2" }, 
         new DBViewModel(this) { Name = "DB3" }, 
         new DBViewModel(this) { Name = "DB4" }, 
         new DBViewModel(this) { Name = "DB5" }, 
         new DBViewModel(this) { Name = "DB6" }, 
         new DBViewModel(this) { Name = "DB7" , IsChecked = true }, 
        }); 


      } 
      return this._availableDatabases; 
     } 
    } 

    public ObservableCollection<DBViewModel> FilteredDatabases 
    { 
     get 
     { 
      if (_filteredDatabases == null) 
       _filteredDatabases = new ObservableCollection<DBViewModel>(new List<DBViewModel>()); 

      return this._filteredDatabases; 
     } 
    } 
} 

public class DBViewModel 
{ 
    private MainViewModel _parentVM; 
    private bool _isChecked; 

    public string Name { get; set; } 

    public DBViewModel(MainViewModel _parentVM) 
    { 
     this._parentVM = _parentVM; 
    } 

    public bool IsChecked 
    { 
     get 
     { 
      return this._isChecked; 
     } 
     set 
     { 
      //This is called when checkbox state is changed 
      this._isChecked = value; 

      //Add or remove from collection on parent VM, perform sorting here 
      if (this.IsChecked) 
       _parentVM.FilteredDatabases.Add(this); 
      else 
       _parentVM.FilteredDatabases.Remove(this); 

     } 
    } 
} 

보기 모델도에서 INotifyPropertyChanged를 구현해야합니다, 나는 그것을 생략.

+0

이것을 사용해 보셨습니까? – jimmyjambles

관련 문제