티스토리 뷰

Previous Platforms

Prism 4.0 Tips

kaki104 2012. 6. 13. 16:33
반응형

01. MVVM에서 DelegateCommand를 사용한 버튼의 사용가능 상태를 그리드뷰에 아이템 선택이 될때마다 확인하기

 

View

 

<Grid>
 <Grid.RowDefinitions>
  <RowDefinition Height="0.145*"/>
  <RowDefinition Height="0.855*"/>
 </Grid.RowDefinitions>


 <!--버튼들-->
 <StackPanel Orientation="Horizontal" Margin="0" HorizontalAlignment="Right" d:LayoutOverrides="HorizontalAlignment">
        <telerik:RadButton Content="취소" Command="{Binding CancelCommand}" Margin="5,0,0,0"/>
        <telerik:RadButton Content="삭제" Command="{Binding DeleteCommand}" Margin="5,0,0,0" />
        <telerik:RadButton Content="복구" Command="{Binding RecoverCommand}" Margin="5,0,0,0" />
 </StackPanel>
 
 <!--그리드뷰-->
 <telerik:RadGridView x:Name="rgvTask"
  AutoGenerateColumns="False"  RowIndicatorVisibility="Collapsed" ShowGroupPanel="False"
  ItemsSource="{Binding Tasks}"  IsReadOnly="True" Grid.Row="1" SelectionMode="Multiple" >
  <i:Interaction.Triggers>
   <i:EventTrigger EventName="SelectionChanged">
    <i:InvokeCommandAction Command="{Binding SelectionChangedTaskCommand}" CommandParameter="{Binding SelectedItems, ElementName=rgvTask}"/>
   </i:EventTrigger>
  </i:Interaction.Triggers>

  <telerik:RadGridView.Columns>
     <telerik:GridViewSelectColumn />
   <telerik:GridViewDataColumn Header="행번호" DataMemberBinding="{Binding No}" />
   <telerik:GridViewDataColumn Header="생성일시" DataMemberBinding="{Binding CreatedOn}" />
   <telerik:GridViewDataColumn Header="종료일시" DataMemberBinding="{Binding EndedTime}" />
   <telerik:GridViewDataColumn Header="설명" DataMemberBinding="{Binding Description}" />
  </telerik:RadGridView.Columns>
 </telerik:RadGridView>
</Grid>


ViewModel

 

//Prism MEF Container 프로젝트
//RaisePropertyChanged는 프리즘에서 제공

private ICommand cancelCommand;
/// <summary>
/// 취소 커맨드
/// </summary>
public ICommand CancelCommand
{
    get
    {
        if (cancelCommand == null)
        {
            cancelCommand = new DelegateCommand(
                () =>
                {
                  //취소 작업
                },
                () => HasSelectedTask);
        }
        return cancelCommand;
    }
}

private ICommand deleteCommand;
/// <summary>
/// 삭제 커맨드
/// </summary>
public ICommand DeleteCommand
{
    get
    {
        if (deleteCommand == null)
        {
            deleteCommand = new DelegateCommand(
                () =>
                {
                  //삭제작업
                },
                () => HasSelectedTask);
        }
        return deleteCommand;
    }
}

private ICommand recoverCommand;
/// <summary>
/// 복구 커맨드
/// </summary>
public ICommand RecoverCommand
{
    get
    {
        if (recoverCommand == null)
        {
            recoverCommand = new DelegateCommand(() => {//복구작업}, () => this.HasSelectedTask);
        }
        return recoverCommand;
    }
}

/// <summary>
/// 선택된 작업존재 여부를 확인 Linq 사용
/// </summary>
public bool HasSelectedTask
{
    get
    {
        var result = this.SelectedTasks.Any();
        return result;
    }
}

private ObservableCollection<Comm.Task> _tasks = new ObservableCollection<Comm.Task>();
/// <summary>
/// 작업 목록
/// </summary>
public ObservableCollection<Comm.Task> Tasks
{
    get { return _tasks; }
    set
    {
        _tasks = value;
        RaisePropertyChanged(() => this.Tasks);
    }
}

private ObservableCollection<Comm.Task> selectedTasks = new ObservableCollection<Comm.Task>();
/// <summary>
/// 선택된 작업들
/// </summary>
public ObservableCollection<Comm.Task> SelectedTasks
{
    get { return selectedTasks; }
    set
    {
        selectedTasks = value;
        RaisePropertyChanged(() => this.SelectedTasks);
    }
}

private ICommand selectionChangedTaskCommand;
/// <summary>
/// 선택 변경 커맨드
/// </summary>
public ICommand SelectionChangedTaskCommand
{
    get
    {
        if (selectionChangedTaskCommand == null)
        {
            selectionChangedTaskCommand = new DelegateCommand<object>(
                obj =>
                {
                    var items = obj as ObservableCollection<object>;
                    if(items == null)
                    {
                        items = new ObservableCollection<object>();
                    }
                    SelectedTasks = new ObservableCollection<Comm.Task>(items.Cast<Comm.Task>());
                    //커맨드의 상태변경 -> 버튼 활성화/비활성화
                    CancelCommand.RaiseCanExecuteChanged();
                    DeleteCommand.RaiseCanExecuteChanged();
                    RecoverCommand.RaiseCanExecuteChanged();
                });
        }
        return selectionChangedTaskCommand;
    }
}

반응형

'Previous Platforms' 카테고리의 다른 글

Korea Bus Information 1.2.0.1 Help  (4) 2012.07.18
Rx Links & Querys  (0) 2012.06.26
Telerik Report Tips  (2) 2012.06.08
개인정보 보호 정책 안내  (2) 2012.05.25
Korea Bus Information 도움말  (2) 2012.05.17
댓글