Is there a way I can use this dependency Property? WPF C#

35 views Asked by At

I am trying to set the increment property of parent class of SKQuantitySpinEdit in a custom control but not able to do so!

  ControlTemplate spinOptLegQuantityTemplate = (ControlTemplate)(grdMultiFills.FindResource("OptLegQuantitySpinEditEntry") as SKControlTemplate).Clone();
                FrameworkElementFactory gridOptLegQuantityControlElement = new FrameworkElementFactory(typeof(SKQuantitySpinEdit));
                spinOptLegQuantityTemplate.VisualTree = gridOptLegQuantityControlElement;

Binding optLegQuantityControlBinding = new Binding(string.Format(BindingRowData_Row + BindingOptLegQuantity, optIndex)) { Mode = BindingMode.TwoWay };
                gridOptLegQuantityControlElement.SetValue(SKQuantitySpinEdit.EditValueProperty, optLegQuantityControlBinding);
                gridOptLegQuantityControlElement.SetValue(SKQuantitySpinEdit.NameProperty, string.Format("OptLegQuantityControl_{0}", count));
                gridOptLegQuantityControlElement.AddHandler(SKQuantitySpinEdit.EditValueChangedEvent, new EditValueChangedEventHandler(m_optLegQuantityChange_EditValueChanged));
                gridOptLegQuantityControlElement.AddHandler(SKQuantitySpinEdit.LoadedEvent, new RoutedEventHandler(LegQtyControl_Loaded));

So What I did I made the Increment Property - Depenedency property like this:

public int Increment
        {
            get { return (int)GetValue(IncrementProperty); }
            set { SetValue(IncrementProperty, value); }
        }

        public static readonly DependencyProperty IncrementProperty =
            DependencyProperty.Register("Increment", typeof(int), typeof(SKSingleSpinner), new PropertyMetadata(0));

and tried setting its value like this

gridOptLegQuantityControlElement.SetValue(SKSingleSpinner.IncrementProperty , 1);

but it's not working! What must be the issue?

1

There are 1 answers

0
BionicCode On

Why do you use the FrameworkElementFactory? It's totally redundant in your case. It is used to e.g. construct framework templates. Even if it was reasonable, the FrameworkElementFactory is deprecated and should no longer be used. You either use XAML (what you should do in the first place as it is the best solution in many aspects) or use the System.Xaml.XamlReader.

The cast of the returned template resource is also not necessary. You don't have to cast it to its original type (SKControlTemplate) before casting it down to the base type (ControlTemplate). Depending on the required type API or based on dependency design rules use one or the other. In this context a cast to ControlTemplate is sufficient.

Because your ControlTemplate is already defined (and does not have to be manually constructed using C#) you can simplify your code as follows:

var spinOptLegQuantityTemplate = (ControlTemplate)grdMultiFills.FindResource("OptLegQuantitySpinEditEntry");
var quantitySpinEditControl = new SKQuantitySpinEdit();
quantitySpinEditControl.Template = spinOptLegQuantityTemplate;

// Set the dependency property using the CLR wrapper
quantitySpinEditControl.Increment = 1;

// Set a dependency property using the CLR accessors
// and interpolated strings instead of string.format   
quantitySpinEditControl.Name = $"OptLegQuantityControl_{count}";

// Register an event handler using the CLR accessors
quantitySpinEditControl.EditValueChanged += m_optLegQuantityChange_EditValueChanged));

// Set a binding
var optLegQuantityControlBinding = new Binding(string.Format(BindingRowData_Row + BindingOptLegQuantity, optIndex)) { Mode = BindingMode.TwoWay };  
quantitySpinEditControl.SetBinding(SKQuantitySpinEdit.EditValueProperty, optLegQuantityControlBinding);

Now, the dependency property is declared on the wrong type. In other words, SKQuantitySpinEdit does not have an IncrementProperty defined.

Instead, the dependency property is owned by the SKSingleSpinner:

DependencyProperty.Register(  
  "Increment",   
  typeof(int),   
  typeof(SKSingleSpinner), // <== Wrong owner. Must be 'SKQuantitySpinEdit' instead   
  new PropertyMetadata(0));

This is probably a typo.