在内联xaml的代码中设置数据源

时间:2013-12-02 12:56:14

标签: c# silverlight xaml data-binding datacontext

请查看以下代码。我正在尝试使用Checkbox模板动态添加数据列。正如您所看到的,我还使用数据上下文代理来绑定复选框的IsChecked值。但是在这种情况下我无法弄清楚如何设置绑定源。

GridViewDataColumn dataColumn = new GridViewDataColumn(); 
                // setup the header
                dataColumn.Header = "sample"; 
                dataColumn.HeaderTextAlignment = TextAlignment.Center;
                dataColumn.IsGroupable = false;
                dataColumn.IsFilterable = true;
                dataColumn.IsSortable = false;
                //// set the width

                dataColumn.MinWidth = 100;
                // setup the binding


                DataContextProxy dcProxy = new DataContextProxy();
                dcProxy.Name = "proxy";
                dcProxy.DataContext = this.DataContext;

            string checkBoxTemplate = string.Format(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" 
                                                        xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
                                                        <CheckBox  IsChecked=""{{Binding Path=DataSource.IsVersionIncluded,Mode=TwoWay}}"" IsThreeState=""False""/>
                                                        </DataTemplate>", dcProxy.DataSource);
            dataColumn.CellTemplate = (DataTemplate)XamlReader.Load(checkBoxTemplate);

            Binding binding = new Binding();


            binding.Source = dcProxy;

            binding.Path = new PropertyPath("DataSource.IsVersionIncluded");
            binding.Mode = BindingMode.TwoWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

            dataColumn.DataMemberBinding = binding;

            //add column to TreeListView
            radTreeListView.Columns.Add(dataColumn);

这是ViewModel中的boolean属性,一旦用户检查/取消选中当前未发生的网格单元格中的复选框,我打算点击该属性。

private bool _isVersionIncluded = false;
    public bool IsVersionIncluded
    {
        get { return _isVersionIncluded; }
        set
        {
            _isVersionIncluded = value;

        }
    }

任何人都可以告诉我在这里失踪了什么?非常感谢。

Omer Javed

1 个答案:

答案 0 :(得分:0)

您没有在您的财产中使用INotifyPropertyChanged Interface

private bool _isVersionIncluded = false;
public bool IsVersionIncluded
{
    get { return _isVersionIncluded; }
    set
    {
        _isVersionIncluded = value;

    }
}