0

I've created custom control (Add/Subtract column on picture) and put it to DataGridTemplateColumn of DataGrid control:

enter image description here

<DataGridTextColumn Binding="{Binding Count}" Header="Count" Width="60"/>
            <DataGridTemplateColumn Width="90" Header="Add/Substract">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <controls:AddSubtractControl HorizontalAlignment="Center" AddSubtractEvent="AddSubtractClick" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

The main task of my custom control, is of course add/subtract some value to actual value located on "Count" column.

When user click "add" button, the control call some event with entered value:

private void AddClick(object sender, MouseButtonEventArgs e)
    {
        AddSubstractEvent(int.Parse(countBox.Text), e);
    }

When I click "add" or "subtract" button, before AddClick method, in some unknown cases the contructor of this control is called first. Thus countBox.Text has default value, not entered before clicking "add" button.

How to deal with this issue? Thanks for any help.

wsc
  • 50
  • 1
  • 8

1 Answers1

0

Since this is a control it should behave like a control. EG a Textbox has a Text dependency property you would bind.

You should have a dependency property which you can then bind to the value. Maybe that's supposed to be an integer so it'd be integer type.

Internally.

Your textbox binds to that dp.

Your + and - buttons increment and decrement the currentvalue of the control's dp.

Andy
  • 11,864
  • 2
  • 17
  • 20