Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
825
Binding a Custom Control, derived from a CellValuePresenter to the Settings.CellValuePresenterStyle of a Field failed
posted

Hello,

i'm trying to create some Editors as Custom Controls which i would use in a datagrid created in codebehind.

All works fine but i wanted to create a Custom Control which derives from the CellValuePresenter class.

The Template of my class is a Button.

The Control looks good but if i want to bind my Custom Control to the Settings.CellValuePresenterStyle of my field, an exception is thrown, which says my Custom Control is not a CellValuePresenter Class.

what can i do?

Here's the code of the Custom Control:

[TemplatePart(Name = "PART_DhpButton", Type = typeof(Button))]
    public class DhpButtonEditor : CellValuePresenter
    {
        static DhpButtonEditor()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DhpButtonEditor), 
                new FrameworkPropertyMetadata(typeof(DhpButtonEditor)));
        }
 
        #region Events
 
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
 
            Button dhpButton = base.GetTemplateChild("PART_DhpButton"as Button;
            if (dhpButton != null)
            {
                dhpButton.Click += new RoutedEventHandler(DhpButton_Click);
            }
        }
 
        public delegate void Click_EventHandler(Object sender, RoutedEventArgs e);
        public event Click_EventHandler Click;
 
        private void DhpButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.Click != null)
                this.Click(sender, e);
        }
 
        #endregion
    }

here's the xaml code:

<Style
        x:Key="{x:Type local:DhpButtonEditor}"
        TargetType="{x:Type igDP:CellValuePresenter}"
        BasedOn="{StaticResource {x:Type igDP:CellValuePresenter}}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
                    <Button
                        Name="PART_DhpButton"
                        Margin="0,0,0,0"
                        Width="{TemplateBinding Width}" 
                        Height="{TemplateBinding Height}"
                        Content="{TemplateBinding Content}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

i try to bind the custom control like this:

Field field = new Field("Button");
field.Width = new FieldLength(50, FieldLengthUnitType.Auto);
 
Style buttonStyle = new Style(typeof(DhpButtonEditor));
field.Settings.CellValuePresenterStyle = buttonStyle;
fieldLayout.Fields.Add(field);

i attached an examle!


THanks for the help.

Jochen

DhpControls.zip