datagrid checked事件中的复选框不会触发wpf mvvm

时间:2015-09-24 09:50:29

标签: c# wpf mvvm datagrid

我的复选框检查事件完全不是触发器。这是我的数据网格代码。我应该如何在wpf mvvm中触发Checked事件。

<Window x:Class="EmployeeManager.View.DataGridDownload"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        Title="DataGridDownload" Height="600" Width="790">
    <Grid>
        <DataGrid HorizontalAlignment="Left" ItemsSource="{Binding TransView}" AutoGenerateColumns="False" Margin="10,62,0,0" VerticalAlignment="Top" Height="497" Width="762">
            <DataGrid.Columns>
                <DataGridTextColumn Header="caseRefNo" Binding="{Binding caseRefNo}" />
                <DataGridTextColumn Header="subjMatr" Binding="{Binding subjMatr}" />
                <DataGridTextColumn Header="Download %" Binding="{Binding incValue}" />
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox  
                                Content="Please Select" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction Command="{Binding CheckCommand}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>
                        </DataTemplate>                        
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding incValue,UpdateSourceTrigger=PropertyChanged}" Background="Red" Foreground="White" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Label Content="{Binding UpdatePercentage}" HorizontalAlignment="Left" Background="Blue" Foreground="White" Margin="10,10,0,0" VerticalAlignment="Top" Width="338" Height="30">
        </Label>
        <Button Content="Button" HorizontalAlignment="Left" Margin="672,20,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>

这是我的视图模型

public class DataGridDownloadViewModel:BindableBase{
public ObservableCollection<tblTransaction> TransList 
{ get; private set; }
public DispatcherTimer dispatchTimer = new DispatcherTimer();
public CollectionView TransView 
{ get; private set; }

public DelegateCommand<object> CheckCommand { get; set; }

private String _UpdatePer;
public String UpdatePercentage
        {
            get { return _UpdatePer; }
            set { SetProperty(ref _UpdatePer, value); }
        }

private string _caseId;
public string CaseID
        {
            get { return _caseId; }
            set { SetProperty(ref _caseId, value); }
        }

private string _isChecked;
public string isChecked
        {
            get { return _isChecked; }
            set { SetProperty(ref _isChecked, value); }
        }

private bool CanExecute(object args)
        {
            return true;
        }

private void CheckBoxChecker(object args)
        {
            //Should Work Here
            // Totally not coming to this function
        }      

public DataGridDownloadViewModel(List<tblTransaction> model)
        {
            CheckCommand = new DelegateCommand<object>(CheckBoxChecker, CanExecute);

            dispatchTimer.Interval = TimeSpan.FromMilliseconds(3000); 
            dispatchTimer.Tick += dispatchTimer_Tick;
            BackGroundThread bgT = Application.Current.Resources["BackGroundThread"] as BackGroundThread;

            bgT.GetPercentChanged += (ss, ee) =>
            {
                UpdatePercentage = bgT.local_percentage.ToString();               
            };

            bgT.GetCaseID += (ss, ee) =>
            {
                CaseID = bgT.local_caseRef;
            };

            TransList =new ObservableCollection<tblTransaction>(model);
            TransView = GetTransCollectionView(TransList);
            TransView.Filter = OnFilterTrans;

            var tokenSource = new CancellationTokenSource();
            var token = tokenSource.Token;

            var cancellationTokenSource = new CancellationTokenSource();

            dispatchTimer.Start();

        }

private void dispatchTimer_Tick(object sender, EventArgs e)
        {
            UpdateDataGrid();
        }       

public void UpdateDataGrid()
        {           
                foreach (tblTransaction tran in TransList)
                {
                    if (tran.caseRefNo == CaseID)
                    {
                        tran.incValue = int.Parse(UpdatePercentage);

                    }
                    else
                    {
                        tran.incValue = tran.incValue;                      
                    }
                }

                TransView.Refresh();           
        }

bool OnFilterTrans(object item)
        {
            var trans = (tblTransaction)item;
            return true;           
        }

        public CollectionView GetTransCollectionView(ObservableCollection<tblTransaction> tranList)
        {
            return (CollectionView)CollectionViewSource.GetDefaultView(tranList);
        }
    }

这是正确的做法吗?为什么checkcommand不是触发器?

被修改

这是我的模特:

    public class tblTransaction
{
    public string caseRefNo { get;set;}
    public string subjMatr { get; set; }
    public int incValue { get; set; }
    public DateTime? longTime { get; set; }

    public bool IsSelected { get; set; }
}

由于

2 个答案:

答案 0 :(得分:2)

尝试以下方法:

<i:InvokeCommandAction Command="{Binding DataContext.CheckCommand, 
    RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />

答案 1 :(得分:1)

下面的代码对我有用,而无需使用上面的答案之类的交互库,只需根据您的情况更改 Command 绑定声明上的 AncestorType 属性,我在上面的示例中使用的是 UserControl

祝你好运!

<DataGridTemplateColumn Width="auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox x:Name="select" HorizontalAlignment="Center"
                      Command="{Binding DataContext.CheckCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                      IsChecked="{Binding Path=Selected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            </CheckBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>