感谢及时回复,看到前搞定了,朋友告诉我用了附加属性弄的,这样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[i] = timeDimensions[i].Substring(0, 1).ToUpperInvariant() + timeDimensions[i].Substring(1, timeDimensions[i].Length - 1);
- bindingColumns.Add(new ColumnConfig { ColumnName = timeDimensions[i], HeaderName = timeDimensions[i], Width = timeDimensions[i].Length * 15, FormatString = "" });
- }
- gridItem.BindingFrozenColumns = dimensions.Count();
- gridItem.BindingColumnsConfig = bindingColumns;
复制代码 |