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?
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, theFrameworkElementFactoryis 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 theSystem.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 toControlTemplateis sufficient.Because your
ControlTemplateis already defined (and does not have to be manually constructed using C#) you can simplify your code as follows:Now, the dependency property is declared on the wrong type. In other words,
SKQuantitySpinEditdoes not have anIncrementPropertydefined.Instead, the dependency property is owned by the
SKSingleSpinner:This is probably a typo.