Custom WPF DataGrid with optional button column

387 views Asked by At

I am creating a custom DataGrid control which has a property, ShowCloneColumn. If you set this property to true the DataGrid should add another column with a Button.

The class I've created derives from DataGrid and implemented a Dependency Property, 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); }
    }

In Generic.xaml I have the following Style.

<!-- 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>

I'm not so strong yet with templates and custom controls so I'm not so sure where to add the button column so that it will be visible when someone uses the CloneRowDataGrid and sets the dependency property ShowCloneColumn to true.

1

There are 1 answers

0
xplat On BEST ANSWER

I had to remove the section from the Generic.xaml style for the DataGrid to properly layout and created the column in code.

        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;
            }
        }