C1DataGrid 怎么动态绑定列结构?
Xaml 里写死是<c1:C1DataGrid >
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn Width="70"
Binding="{Binding ClientId}"
Header="ID" />
<c1:DataGridTextColumn Width="200"
Binding="{Binding ClientName}"
Header="客户" />
<c1:DataGridTextColumn Width="155"
Binding="{Binding ClientCreateDate}"
Header="创建时间" />
</c1:C1DataGrid.Columns>
</c1:C1DataGrid>
C1DataGrid的 Columns 不是依赖属性,那怎么动态绑定列结构呢?也就是说要显示的表是不固定的 回复 1楼heyixiaoran的帖子
我写个基本的思路给你吧。
1.比如说数据源是这样的,里面有FirstName和LastName两列:
public class Property : INotifyPropertyChanged
{
public Property(string name, object value)
{
Name = name;
Value = value;
}
public string Name { get; private set; }
public object Value { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
public class Record
{
private readonly ObservableCollection<Property> properties = new ObservableCollection<Property>();
public Record(params Property[] properties)
{
foreach (var property in properties)
Properties.Add(property);
}
public ObservableCollection<Property> Properties
{
get { return properties; }
}
}
2.XMAL的代码绑定数据源应该这样写:
<c1:C1DataGridAutoGenerateColumns="false"
ItemsSource="{Binding Path=Records}" />
3.初始化数据,然后动态绑定列:
var records = new ObservableCollection<Record>();
records.Add(new Record(new Property("FirstName", "Paul"), new Property("LastName", "Stovell")));
records.Add(new Record(new Property("FirstName", "Tony"), new Property("LastName", "Black")));
var columns = records.First()
.Properties
.Select((x, i) => new { Name = x.Name, Index = i })
.ToArray();
foreach (var column in columns)
{
var binding = new Binding(string.Format("Properties[{0}].Value", column.Index));
C1.WPF.DataGrid.DataGridTextColumn textColumn = new C1.WPF.DataGrid.DataGridTextColumn();
textColumn.Header = column.Name;
textColumn.Binding = binding;
this.c1DataGrid1.Columns.Add(textColumn);
}
其他的比如模板设计,你可以写代码实现。 感谢及时回复,看到前搞定了,朋友告诉我用了附加属性弄的,这样VM里不会出现任何和控件相关的属性,PCL的VM和View 不在一个项目里,夹杂了一些业务上的东西,不是太全,但是大概意思到了
public class ColumnConfig
{
public string HeaderName { get; set; }
public string ColumnName { get; set; }
public string FormatString { get; set; }
}
public static class C1FlexGridExtensions
{
public static ObservableCollection<ColumnConfig> GetBindingColumns(DependencyObject obj)
{
return (ObservableCollection<ColumnConfig>)obj.GetValue(BindingColumnsProperty);
}
public static void SetBindingColumns(DependencyObject obj, ObservableCollection<ColumnConfig> value)
{
obj.SetValue(BindingColumnsProperty, value);
}
// Using a DependencyProperty as the backing store for BindingColumns.This enables animation, styling, binding, etc...
public static readonly DependencyProperty BindingColumnsProperty =
DependencyProperty.RegisterAttached("BindingColumns", typeof(ObservableCollection<ColumnConfig>), typeof(C1FlexGridExtensions), new PropertyMetadata(null, BindingColumnsChanged));
private static void BindingColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var bindingColumns = e.NewValue as ObservableCollection<ColumnConfig>;
if (bindingColumns != null)
{
var c1FlexGrid = d as C1FlexGrid;
if (c1FlexGrid != null)
{
foreach (var bindingColumn in bindingColumns)
{
c1FlexGrid.Columns.Add(new Column
{
Header = bindingColumn.HeaderName,
Binding = new Binding(bindingColumn.ColumnName),
Width = new GridLength(bindingColumn.Width),
Format = bindingColumn.FormatString
});
}
}
}
}
public static int GetBindingFrozenColumns(DependencyObject obj)
{
return (int)obj.GetValue(BindingFrozenColumnsProperty);
}
public static void SetBindingFrozenColumns(DependencyObject obj, int value)
{
obj.SetValue(BindingFrozenColumnsProperty, value);
}
// Using a DependencyProperty as the backing store for BindingFrozenColumns.This enables animation, styling, binding, etc...
public static readonly DependencyProperty BindingFrozenColumnsProperty =
DependencyProperty.RegisterAttached("BindingFrozenColumns", typeof(int), typeof(C1FlexGridExtensions), new PropertyMetadata(0, BindingFrozenColumnsChanged));
private static void BindingFrozenColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null)
{
var c1FlexGrid = d as C1FlexGrid;
if (c1FlexGrid != null)
{
c1FlexGrid.FrozenColumns = Convert.ToInt32(e.NewValue);
}
}
}
}
<c1:C1FlexGrid x:Name="_flexFinancial"
HorizontalAlignment="Left"
AutoGenerateColumns="False"
IsReadOnly="true"
ItemsSource="{Binding GridItemSource}"
MaxColumnWidth="150"
MinColumnWidth="10"
Style="{StaticResource C1FlexGridStyle1}"
common:C1FlexGridExtensions.BindingColumns="{Binding BindingColumnsConfig}"
common:C1FlexGridExtensions.BindingFrozenColumns="{Binding BindingFrozenColumns}">
<c1:C1FlexGridFilterService.FlexGridFilter>
<c1:C1FlexGridFilter />
</c1:C1FlexGridFilterService.FlexGridFilter>
</c1:C1FlexGrid>
public class GridItemInfo : IItemInfo
{
public int Id { get; set; }
public double ItemWidth { get; set; }
public double ItemHeight { get; set; }
public string ItemTitle { get; set; }
public ObservableCollection<SponsorData> GridItemSource { get; set; }
public ObservableCollection<ColumnConfig> BindingColumnsConfig { get; set; }
public int BindingFrozenColumns { get; set; }
public bool CanSetting { get; set; }
public bool CanCopy { get; set; }
public bool CanExportExcel { get; set; }
public bool CanExportPicture { get; set; }
public bool CanLookOriginalData { get; set; }
public bool CanDelete { get; set; }
}
for (int i = 0; i < timeDimensions.Count(); i++)
{
timeDimensions = timeDimensions.Substring(0, 1).ToUpperInvariant() + timeDimensions.Substring(1, timeDimensions.Length - 1);
bindingColumns.Add(new ColumnConfig { ColumnName = timeDimensions, HeaderName = timeDimensions, Width = timeDimensions.Length * 15, FormatString = "" });
}
gridItem.BindingFrozenColumns = dimensions.Count();
gridItem.BindingColumnsConfig = bindingColumns;
感谢及时回复,看到前搞定了,朋友告诉我用了附加属性弄的,这样VM里不会出现任何和控件相关的属性,PCL的VM和View 不在一个项目里,夹杂了一些业务上的东西,不是太全,但是大概意思到了
public class Co......
heyixiaoran 发表于 2014-11-28 18:14:00 http://gcdn.gcpowertools.com.cn/images/common/back.gif
感谢您的反馈,为其他人解决问题提供思路。
稍后会赠送金币作为奖励。
也欢迎你为本次服务评分。
http://gcdn.gcpowertools.com.cn/attachment.aspx?attachmentid=10062 请问老师:ItemsSource="{Binding Path=Records}" 中的 Records 如何定义?
这个Records就是你在VM中定义的的那个ObservableCollection<Record>
页:
[1]