【问题标题】:Custom WPF DataGrid with optional button column带有可选按钮列的自定义 WPF DataGrid
【发布时间】:2015-06-17 04:03:16
【问题描述】:

我正在创建一个具有属性 ShowCloneColumn 的自定义 DataGrid 控件。如果您将此属性设置为 true,则 DataGrid 应添加另一个带有 Button 的列。

我创建的类派生自 DataGrid 并实现了一个依赖属性 ShowCloneColumn。

    public static readonly DependencyProperty ShowCloneColumnProperty =
            DependencyProperty.Register("ShowCloneColumn", 
            typeof(bool),
            typeof(CloneRowDataGrid),
            new FrameworkPropertyMetadata(false, 
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                OnShowCloneColumnPropertyChanged));

    public bool ShowCloneColumn
    {
        get { return (bool) GetValue(ShowCloneColumnProperty); }
        set { SetValue(ShowCloneColumnProperty, value); }
    }

在 Generic.xaml 我有以下样式。

<!-- Somewhere in here a button column should be declared? -->
<Style TargetType="{x:Type uiControls:CloneRowDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type uiControls:CloneRowDataGrid}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ShowCloneColumn" Value="True">
                            <!-- Show clone column, a column with a button -->
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我在模板和自定义控件方面还不是很强大,所以我不太确定在哪里添加按钮列,以便在有人使用 CloneRowDataGrid 并将依赖项属性 ShowCloneColumn 设置为 true 时可见。

【问题讨论】:

    标签: c# wpf xaml datagrid custom-controls


    【解决方案1】:

    我必须从 Generic.xaml 样式中删除该部分,以便 DataGrid 正确布局并在代码中创建列。

            protected override void OnInitialized(EventArgs e)
            {
                base.OnInitialized(e);
    
                CloneColumn.Visibility = ShowCloneColumn ? Visibility.Visible : Visibility.Hidden;
            }
    
            private DataGridTemplateColumn _cloneColumn;
            private DataGridTemplateColumn CloneColumn
            {
                get
                {
                    if (_cloneColumn == null)
                    {
                        _cloneColumn = new DataGridTemplateColumn
                        {
                            Header = string.Empty,
                            Visibility = ShowCloneColumn ? Visibility.Visible : Visibility.Hidden
                        };
                        FrameworkElementFactory buttonFactory = new FrameworkElementFactory(typeof (Button));
                        buttonFactory.SetValue(Button.ContentProperty, "Clone");
                        buttonFactory.AddHandler(Button.ClickEvent, new RoutedEventHandler(CloneButtonClicked));
                        DataTemplate textTemplate = new DataTemplate {VisualTree = buttonFactory};
                        _cloneColumn.CellTemplate = textTemplate;
                        Columns.Add(_cloneColumn);
                    }
                    return _cloneColumn;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 2019-07-23
      • 2013-03-10
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      相关资源
      最近更新 更多