简体   繁体   中英

Event handler can't be found in MVVM

I have a silverlight application. One of StackPanel will display the table. The first column is a check box.

<telerik:RadGridView.Columns>
                <telerik:GridViewColumn Width="80" Header="Complete" HeaderTextAlignment="Center" TextAlignment="Center">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox HorizontalAlignment="Center" IsChecked="{Binding Something, Converter={StaticResource ShortToBooleanConverter}}" Checked="Complete_Checked"></CheckBox>
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>

What I want is that once I click the box, a message box with Y/N pops up. I do have a Complete_Checked method in MVVM. But I get the error

Event handler 'Complete_Checked` not found on class.....

You can't use click event handlers with MVVM you need to use CommandBinding or DataBinding depending on what you're doing.

With your example you'll use data binding. You want to bind to the checkbox dependency property called IsChecked . You'll also want to use the Mode of TwoWay. This will allow the UI to update the bound property when it changes.

<CheckBox IsChecked="{Binding CheckBoxIsChecked, Mode=TwoWay}">

Then in your object model not viewmodel

private bool _checkBoxIsChecked;

public bool CheckBoxIsChecked
{
   get{ return _checkBoxIsChecked;}
   set{_checkBoxIsChecked = value; OnPropertyChanged("CheckBoxIsChecked"); }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM