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
I have been looking into your issue and I believe that you would like to define you own value editor, not new CellValueEditor. I have modified your sample application(DhpControlsModified.zip) in order to achieve the desired functionality. I would like to suggest you define styles for CellValuePresenter in XAML as it is shown in the following forum thread :
http://help.infragistics.com/Help/NetAdvantage/WPF/2011.1/CLR4.0/html/InfragisticsWPF4.DataPresenter.v11.1~Infragistics.Windows.DataPresenter.FieldSettings~CellValuePresenterStyle.html
If you have any other questions on this matter, feel free to ask.
Thank you very much for the example and the link!
This solved my problem!
Sincerely,Jochen
Hello Nikolov,
i've another question which belongs to this topic:
First i tried to add a click event to my custom-control-button-field. This worked fine!
// Field "Button1" field = new Field("Button1"); field.Width = new FieldLength(50, FieldLengthUnitType.Auto); field.Settings.EditorType = typeof(DhpButtonEditor); Style buttonStyle = new Style(typeof(DhpButtonEditor)); buttonStyle.Setters.Add(new EventSetter(DhpButtonEditor.ClickEvent, new RoutedEventHandler(ButtonClicked))); buttonStyle.Seal(); field.Settings.EditorStyle = buttonStyle; fieldLayout.Fields.Add(field);Then i tried to set the Value Property when the field is created:
buttonStyle.Setters.Add(new Setter(DhpButtonEditor.ValueProperty, "Btn 2")); // <-- Why does this not work? but the value "Btn 2" isn't set to all the button contents in this button field.I attached the edited example!Thanks for your help,Jochen
I am just checking if you require any further assistance on the matter.