【问题标题】:Event handler can't be found in MVVM在 MVVM 中找不到事件处理程序
【发布时间】:2016-10-31 00:53:48
【问题描述】:

我有一个 Silverlight 应用程序。 StackPanel 之一将显示表格。第一列是一个复选框。

<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>

我想要的是,一旦我单击该框,就会弹出一个带有 Y/N 的消息框。我在 MVVM 中有一个 Complete_Checked 方法。但我得到了错误

在课堂上找不到事件处理程序“Complete_Checked”.....

【问题讨论】:

    标签: c# silverlight mvvm telerik


    【解决方案1】:

    您不能在 MVVM 中使用 click 事件处理程序,您需要使用 CommandBindingDataBinding,具体取决于您在做什么。

    在您的示例中,您将使用数据绑定。您想绑定到名为IsChecked 的复选框依赖属性。您还需要使用双向模式。这将允许 UI 在其更改时更新绑定的属性。

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

    然后在您的对象中 model 而不是 viewmodel

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

    【讨论】:

    • 那么this answer from telerik 错了吗?
    • @Bigeyes 他们没有采用 MVVM 方法。他们正在使用 Xaml 的背后代码。放弃是他们可以访问控制权。您会看到 OP 在哪里写入“radGridView1”,这是他们可以访问的控件。他或她正在 Xaml.CS 文件中进行操作。
    • 问题是我已经绑定了“Something”,我无法控制它。我可以绑定两个项目吗?
    • 将视图模型连接到视图的方法是进入 Xaml.cs 构造函数并说 this.DataContext = new MyViewModel()
    • 当然。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多