Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Transfer User Control to a WPF custom control

$
0
0

Ok, I have a user control, which I want to transfer into a WPF custom control.

The user control has code behind file, so I can directly register to events of control: For Example:

<TextBoxGrid.Column="0"Name="valueBox"TextAlignment="Right"TextChanged="valueBox_TextChanged"PreviewKeyDown="valueBox_PreviewKeyDown"/>

And the corresponding valueBox_TextChanged event in the code behind is:

 private void valueBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        var textBox = (TextBox)sender;
        var positiveWholeNum = new Regex(@"^-?\d+$");
        if (!positiveWholeNum.IsMatch(textBox.Text) || int.Parse(textBox.Text) < 0)
        {
            textBox.Text = "0";
        }
        NumericValue = Convert.ToInt32(textBox.Text);

        RaiseEvent(new RoutedEventArgs(ValueChangedEvent));

    }

Now, when I try to turn this into a custom control, I have a Theme Folder, with a Generic.Xaml file. And file: MyCustomControl.cs

In Generic.Xaml, I've written (I derive from Control)

<TextBoxGrid.Column="0"Name="valueBox"TextAlignment="Right"/>

and in the cs file:

public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        AttachTextBox();
    }


 protected TextBox TextBox;

    private void AttachTextBox()
    {
        var textBox = GetTemplateChild("valueBox") as TextBox;

        if (textBox != null)
        {
            TextBox = textBox;
        }
    }

Now, how do I write a replacement for valueBox_TextChanged which is written in the user control?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>