2013-06-21 2 views
2

IsChecked이라는 부울 속성이있는 클래스가 있습니다. 이 클래스의 컬렉션은 내 viewmodel에 존재합니다. 내보기에서 DataGrid를이 컬렉션에 바인딩했습니다. 보기의 확인란이 변경되면 내 viewmodel에서 메서드를 호출해야합니다. 나는 클래스에 INotifyPropertyChanged을 구현했으며 박스를 체크했을 때 발사하고있다.하지만 내 viewmodel에서 메소드를 호출하는 방법을 모른다. 여기 모델 클래스의 속성이 변경되었음을 viewmodel 컬렉션에 알리는 방법

은 ...
private ObservableCollection<AccountComponent> _accountComponents; 
    private string _accountStatus; 

    public ObservableCollection<AccountComponent> AccountComponents 
    { 
     get { return _accountComponents; } 
     set 
     { 
      _accountComponents = value; 
      NotifyPropertyChanged("AccountComponents"); 
      CalculateComponentTotal(); 
     } 
    } 

여기 뷰에서 내 XAML의 ... 여기
public class AccountComponent : INotifyPropertyChanged 
{ 
    public string Name { get; set; } 
    public decimal Amount { get; set; } 

    private bool _isChecked; 
    public bool IsChecked 
    { 
     get { return _isChecked; } 
     set 
     { 
      _isChecked = value; 
      NotifyPropertyChanged("IsChecked"); 
     } 
    } 

    public bool Enabled { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 
} 

내 뷰 모델에서 컬렉션의 ... 내 모델 클래스의

<DataGrid ItemsSource="{Binding AccountComponents}" AutoGenerateColumns="False" Margin="5"> 
      <DataGrid.Columns> 
       <DataGridTemplateColumn> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Grid> 
           <CheckBox IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsEnabled="{Binding Enabled}"/> 
          </Grid> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTextColumn Binding="{Binding Name}" Header="Component" Width="*" IsReadOnly="True" ElementStyle="{DynamicResource TextBlock-Sketch}"/> 
       <DataGridTextColumn Binding="{Binding Amount,StringFormat={}{0:C}}" IsReadOnly="True" Header="Charge" ElementStyle="{DynamicResource TextBlock-Sketch}"> 
        <DataGridTextColumn.CellStyle> 
         <Style> 
          <Setter Property="TextBlock.TextAlignment" Value="Right"/> 
         </Style> 
        </DataGridTextColumn.CellStyle> 
       </DataGridTextColumn> 
      </DataGrid.Columns> 
     </DataGrid> 

답변

0

AccountComponent은 INPC를 구현하므로 VM에 IsChecked 속성을 사용할 수 있습니다.

는 VM 생성자에서 말 :

AccountComponents = new ObservableCollection<AccountComponent>(); 
AccountComponents.CollectionChanged += AccountComponentsOnCollectionChanged; 

... 

private void AccountComponentsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) { 
    if (args.NewItems != null && args.NewItems.Count != 0) 
    foreach (AccountComponent account in args.NewItems) 
     account.PropertyChanged += AccountOnPropertyChanged; 

    if (args.OldItems != null && args.OldItems.Count != 0) 
    foreach (AccountComponent account in args.OldItems) 
     account.PropertyChanged -= AccountOnPropertyChanged; 
} 

private void AccountOnPropertyChanged(object sender, PropertyChangedEventArgs args) { 
    if (args.PropertyName == "IsChecked") 
    // Invoke Your VM Function Here 
} 

을해야한다고. 뷰 모델에서

<CheckBox> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Checked"> 
       <i:InvokeCommandAction Command="{Binding CheckedCommand}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </CheckBox> 

:

public ICommand CheckedCommand 
    { 
     get 
     { 
      return new DelegateCommand(OnChecked);//Delegate command is the Implemntation of Icommand Interface 
     } 
    } 

public void OnLogin(object param) 
    { 
     //code for you checked event 
    } 

희망이 뜻을 다음과 같은 코드를 추가 체크 박스 다음 namspace를 추가 .. 이제

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 

:

XAML에서
관련 문제