2011-12-27 2 views
1

ListView 내에서 체크 상자에 대해 양방향 바인딩을 만들고 싶습니다. 뷰 모델 클래스에서 CheckBox 양방향 바인딩이 작동하지 않습니다.

public class Product 
{ 
    public bool IsSelected { get; set; } 
    public string Name { get; set; } 
} 

가 나는 제품의 관찰 모음이 : 이것은 내 제품 클래스입니다 내가 ListView에 그리드를

private ObservableCollection<Product> _productList; 
    public ObservableCollection<Product> ProductList 
    { 
     get 
     { 
      return _productList; 
     } 
     set 
     { 
      _productList = value; 
     } 
    } 

    public MainViewModel() 
    { 
     ProductList = new ObservableCollection<Product> 
          { 
           new Product {IsSelected = false, Name = "Not selected"}, 
           new Product {IsSelected = true, Name = "Selected"}, 
           new Product {IsSelected = true, Name = "Selected"} 
          }; 
    } 
} 

그리고 마지막을 나의 제품 목록 바인딩 :

<Grid> 
    <ListView Height="120" HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        SelectionMode="Multiple" 
        ItemsSource="{Binding ProductList}" > 
     <ListView.View> 
      <GridView> 
       <GridViewColumn Width="40"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
       <GridViewColumn Width="120" Header="Product Name" DisplayMemberBinding="{Binding Path=Name}" /> 
      </GridView> 
     </ListView.View> 
    </ListView> 
</Grid> 

나는 체크 박스를 체크하거나 체크하지 않을 때 결코 setter의 라인에 도착하지 않는이 어플리케이션을 디버깅한다. 이 코드의 잘못된 점은 무엇입니까? 미리 감사드립니다.

+0

http://stackoverflow.com/a/504936/440030 –

답변

1

CheckBox를 IsSelected 속성에 바인딩합니다. 이 속성은 auto implemented property으로 구현됩니다. 디버거에서 setter 또는 getter에서 중단되지 않습니다. 코드에서 어떤 문제도 볼 수 없으며 코딩 한 것처럼 작동해야합니다.

2

우선 확인 뷰 모델 및 제품 클래스에 INotifyPropertyChanged 이벤트를 구현해야합니다 당신을 일에 결합하는 두 가지 방법을 당신이 DataContext을 설정하는 즉시 또한

통지되는 속성보기에 약간의 변화를 만들어가있을 때 보기의 제대로
view.DataContext = yourViewModel; 

Fischermaen 당신이

public class Product 
    { 
     private bool isSelected; 

     public bool IsSelected 
     { 
      get { return isSelected; } 
      set { isSelected = value; } 
     } 
    } 
을 디버깅 할 경우이 같은 뭔가를해야 할 재산의 종류를 디버깅 할 수 없습니다 언급 한 바와 같이
관련 문제