【问题标题】:Setting Data Source in code behind for inline xaml在代码中为内联 xaml 设置数据源
【发布时间】:2013-12-02 12:56:14
【问题描述】:

请看下面的代码。我正在尝试使用 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 中的布尔属性,一旦用户选中/取消选中当前未发生的网格单元格中的复选框,我打算点击该属性。

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

        }
    }

请谁能告诉我这里缺少什么?非常感谢。

奥马尔·贾维德

【问题讨论】:

    标签: c# silverlight xaml data-binding datacontext


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 2013-09-01
      相关资源
      最近更新 更多