[英]Setting Data Source in code behind for inline xaml
請看下面的代碼。 我正在嘗試使用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;
}
}
請有人能告訴我我在這里想念什么嗎? 非常感謝。
Omer Javed
您沒有在媒體資源中使用INotifyPropertyChanged接口
private bool _isVersionIncluded = false;
public bool IsVersionIncluded
{
get { return _isVersionIncluded; }
set
{
_isVersionIncluded = value;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.