简体   繁体   English

WPF与Datagrid的绑定属性

[英]WPF binding properties to Datagrid

I have bounded a ObservableCollection to a ItemSource to a DataGrid, however, I want to retrieve (via a setter) individual properties via the ViewModel. 我已经将ObservableCollection绑定到DataGrid的ItemSource,但是,我想通过ViewModel检索(通过设置器)单个属性。

Ok sounds confusing so will explain. 好的声音令人困惑,因此将进行解释。

in my ObservableCollection, I have a property called "Active" so I want this element to be set when a user clicks on or off on a the checkbox in the DataGrid. 在我的ObservableCollection中,我有一个名为“活动”的属性,因此我希望在用户单击或关闭DataGrid中的复选框时设置此元素。

so the XAML 所以XAML

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <CheckBox IsChecked="{Binding Active, Mode=TwoWay}" HorizontalAlignment="Center"></CheckBox>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

And I want this to trigger this code in the ViewModel when the box is unchecked or checked 当框未选中或选中时,我希望它在ViewModel中触发此代码

private bool m_Active = false;

public bool Active
{
    get { return m_Active; }
    set
    {
        m_Active = value;

        OnPropertyChanged("Active");
    }
}

but even with two way mode on, it doesn't. 但是即使启用双向模式,也不会。 Any reasons why? 有什么原因吗?

Note: On the SelectedItem property of the DataGrid I can get the SelectedRow , so basically I want the selected Individual property! 注意:在DataGrid的SelectedItem属性上,我可以获取SelectedRow,因此基本上我想要选择的Individual属性!

Thanks 谢谢

It sounds like you are confusing where the datagrid is looking for the 'Active' property. 听起来您好像在混淆数据网格在哪里寻找“ Active”属性。 Since the datagrid is bound to an Observable Collection, the objects inside the observable collection need to have the 'Active' property on them, not the view model used for the view. 由于数据网格已绑定到Observable集合,因此Observable集合内的对象需要具有'Active'属性,而不是用于视图的视图模型。 If, however, you actually want to bind all the rows of a datagrid to a single property on the view model, you'll need to look up the ancestor tree to find the control's data context and then bind to the 'Active' property: 但是,如果您实际上想将数据网格的所有行绑定到视图模型上的单个属性,则需要查找祖先树以找到控件的数据上下文,然后绑定到“ Active”属性:

<CheckBox IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.Active, Mode=TwoWay}" HorizontalAlignment="Center"></CheckBox>

But, I imagine, you are looking to bind to the 'Active' property of the object in the observable collection. 但是,我想,您正在寻求绑定到可观察集合中对象的'Active'属性。 Check your output window when you are running your application and you should see a binding error if that property doesn't exist on the object. 运行应用程序时检查输出窗口,如果该属性在对象上不存在,则应该看到绑定错误。

Try using a CellEditingTemplate 尝试使用CellEditingTemplate

<DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Active, Mode=TwoWay}" HorizontalAlignment="Center"></CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>

Hope that helps 希望能有所帮助

您是否尝试过设置UpdateSourceTrigger?

<CheckBox IsChecked="{Binding Active, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center"></CheckBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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