13 Replies - 1787 Views - Last Post: 19 January 2017 - 07:41 AM Rate Topic: -----

#1 STP_Captain_Slow   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 08-October 10

WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 07:26 AM

Hi

I am very new to WPF (only started yesterday) however i have been working with WinForms for a while.

I am having difficulty working out how to create a custom control compared to WinForms. I want to create a custom TextBox where i add the Property "IsRequired" (Boolean), and a Trigger to set the TextBox Background Color to White OnEnter.

This is what i have tried so far (I haven't looked at adding the Trigger yet):

Add -> New Item -> Custom Control (WPF)

        
    Imports System.Windows.Controls.Primitives


    Public Class C_TextBox
    Inherits TextBox

    Shared Sub New()
        DefaultStyleKeyProperty.OverrideMetadata(GetType(C_TextBox), New FrameworkPropertyMetadata(GetType(C_TextBox)))
        IsRequiredProperty = DependencyProperty.Register("IsRequired", GetType(Boolean), GetType(C_TextBox))
    End Sub

    Shared IsRequiredProperty As DependencyProperty

    Public Property IsRequired() As Boolean
        Get
            Return MyBase.GetValue(IsRequiredProperty)
        End Get
        Set(value As Boolean)
            MyBase.SetValue(IsRequiredProperty, value)
        End Set
    End Property

End Class



On the Form i want to display this TextBox...

xmlns:MyNamespace="clr-namespace:<name of project>"

<!--This shows as default when dragged from Toolbox-->
<MyNamespace:C_TextBox HorizontalAlignment="Left" Height="100" Margin="17,13,0,-110" Grid.Row="2" TextWrapping="Wrap" Text="C_TextBox" VerticalAlignment="Top" Width="100"/>




... But when i select this from the Toolbox nothing really happens...

I'm not sure if the above is correct or if i am selecting the correct item type, or then calling it correcting in XAML on the form i want to use

I have spent a long time looking through forums etc and i have just got myself more confused.

Many Thanks for any idiot proof step by step information

This post has been edited by andrewsw: 18 January 2017 - 10:01 AM
Reason for edit:: Removed VB.NET from topic title, it is redundant


Is This A Good Question/Topic? 0
  • +

Replies To: WPF Custom Control - New TextBox Property

#2 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6552
  • View blog
  • Posts: 26,565
  • Joined: 12-December 12

Re: WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 08:20 AM

In what way does nothing really happen? Do you get a textbox? Does your property appear in the properties window?
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6552
  • View blog
  • Posts: 26,565
  • Joined: 12-December 12

Re: WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 08:27 AM

Also,you should spend more than a day getting familiar with WPF before looking at custom controls. WPF is very different to WinForms.
Was This Post Helpful? 0
  • +
  • -

#4 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 08:39 AM

Hello,

I have never really gotten into WPF however your doing something peculiar that I have noticed.

Your dependency property, your declaring it as a shared property (which is correct) and the registering it every time you create a new control, you don't want to do this. Ideally you only want to register it once.

Typically you will see the dependency Property declared and registered on one line so that it will only fire once.

So your line 12 becomes
    Public Shared ReadOnly IsRequiredProperty As DependencyProperty = DependencyProperty.Register("IsRequired", GetType(Boolean), GetType(C_TextBox))


This could be causing you an issue, but i doubt it, it is more likely just a bit pointless to keep re-registering, in CSLA you register property information and if you do that twice it will throw an issue.

I have just created custom textbox control creating a class and inheriting a textbox and that worked (I am not saying do it that way) however when I attempted to do it as a WPF Control I used your code and I was getting a issue that the types didn't match.

Like I said I haven't got a lot of experience in WPF, however this feels like a issue I used to get in WinForms when I didn't know what I was doing and attempt to get a user control to inherit a type in the class file while the designer class was still pointing to UserControl, it just didn't know what to do.

Is this what you are getting?

When I was doing this I read through the MSDN documentation Try it: Create a custom WPF control and if you read the example, all they are doing is creating a class within the main window class, which worked for me.

However Andrew is right, if you are only just getting into WPF like myself i would not recommend attempting to do custom controls.

This post has been edited by maceysoftware: 18 January 2017 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#5 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 08:47 AM

I actually take back what I said, Your Sub New is shared, therefore its the same, just never seen it done that way before.

Also I was attempting to inherit on a user control instead of a custom control, when I did it with a custom control using your code this works.

This post has been edited by maceysoftware: 18 January 2017 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#6 STP_Captain_Slow   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 08-October 10

Re: WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 08:59 AM

View Postandrewsw, on 18 January 2017 - 08:27 AM, said:

Also,you should spend more than a day getting familiar with WPF before looking at custom controls. WPF is very different to WinForms.


Yea I am finding that out, however i have just built a program in WinForms which i am trying to recreate in WPF while it is fresh in my mind, main reason is because there seems to be some cool features in WPF and i believe it handles when users have there system set to 125% and 150% font better than winforms as my controls font was badly affected when working @ 125%. However the design aspect is way more advanced than WinForms.

It shows in the Toolbox and when i drag it on to the work space i can see all the properties you would expect for a Text Box (including my IsRequired Property) however it just looks like a basic shape that you can move around (no text box outline) and when i Run the program you can't see the shape and you can't click it to start typing (see attached).

Attached image(s)

  • Attached Image
  • Attached Image

This post has been edited by STP_Captain_Slow: 18 January 2017 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#7 STP_Captain_Slow   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 08-October 10

Re: WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 09:06 AM

View Postmaceysoftware, on 18 January 2017 - 08:47 AM, said:

I actually take back what I said, Your Sub New is shared, therefore its the same, just never seen it done that way before.

Also I was attempting to inherit on a user control instead of a custom control, when I did it with a custom control using your code this works.


Thanks Macey

I understand what you mean and looking at all the stuff i have seen i know WPF is a lot more detailed and technical, however creating a Custom Control in WinForms was relatively straight forward, put the code in a class, compile so it shows then drop it on the form and you're away.
Was This Post Helpful? 0
  • +
  • -

#8 STP_Captain_Slow   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 08-October 10

Re: WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 09:12 AM

Also if you have any suggestions of good tutorials for WPF and/or sample code/ projects to look at would be really helpful.
Was This Post Helpful? 0
  • +
  • -

#9 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 03:33 PM

Ok so I have done some reading and that is about it, this tutorial I had to read about 4 times before getting it however I think I now get it.

The tutorial is very brief and leaves much for you to wonder, however there are some guesses I can now make after reading this.

I am sure you know this however in WPF every control has a style, its what makes a button looks like a button, along with what happens when you hover over a button, all this logic is wrapped up in a style, well at least the default behaviour is, I am sure you can overwrite this for a button on the fly if you so wished, however I wonder off topic.

So in your class your creating a new custom control, and inheriting it to be type of Textbox. along with this your overriding the DefaultStyleKey, to pick up a style associated with your control. - at least this is what think this is doing, remember I don't have much experience with WPF either so really don't hold me to this.

 DefaultStyleKeyProperty.OverrideMetadata(GetType(C_TextBox), New FrameworkPropertyMetadata(GetType(C_TextBox)))



So when the WPF application goes to render it is going to go looking for a style for your type of control, not of the base control but of your actual custom control type, well I don't know about you but in my example here I haven't created a style for my custom control to use.... oh I think we have just found out why our control is not being shown at runtime or really at design time.

back to google I go, searching for WPF Custom Control Styles I find this article

Quote

I have a custom control with a button in its template. Template is in my.xaml file referenced in themes\generic.xaml.


After having quick look in the generic.xaml I can see a style outline has been created for the custom control. I can also see why its not showing, its because apart from a few sets its not doing a great deal, now I don't really know a lot about style or WPF I have really only followed a few tutorial from what Andrew has posted.

Now ideally you don't want to re-design the whole textbox behaviour, ideally you will want to inherit the textbox style into your own style. Now with styles you don't really inherit (at least that is not the term they use) instead you make your style based on another style.

Now I could just give you a code dump however that is not going to help you learn just like it wouldn't of helped me, I have linked you to the articles on the website which I have learnt from this evening so really give it a go and see where you get up to, this is a little hiccup that I came across that I haven't mentioned to do with the style because again you don't learn without a bit of a challenge, however any issues come back and I can point you In the right direction.

This post has been edited by maceysoftware: 18 January 2017 - 03:37 PM

Was This Post Helpful? 2
  • +
  • -

#10 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1502
  • View blog
  • Posts: 3,801
  • Joined: 01-February 13

Re: WPF Custom Control - New TextBox Property

Posted 18 January 2017 - 05:58 PM

I too have read over the msdn documents on this and a few other links. I believe maceysoftware is pretty much on the money from my quick research. By overriding the DefaultStyleKeyProperty, you are telling your control to use a custom theme/style that you have created for your control. If it does not exist, there is basically no visual style to your control.

That can be tested by commenting that line of code out and rebuilding the application. The TextBox will then take on the look of a TextBox again.

Perhaps following through some of the msdn documents in this link will help a bit if you have not read through them already.
WPF - Properties How-to Topics

Also, some of the links in this google search result might help you advance on using a custom style for your control if that is what you really want to do.
Google Search - "create a custom style for wpf control"
Was This Post Helpful? 2
  • +
  • -

#11 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 353
  • View blog
  • Posts: 1,533
  • Joined: 07-September 13

Re: WPF Custom Control - New TextBox Property

Posted 19 January 2017 - 02:52 AM

Quote

That can be tested by commenting that line of code out and rebuilding the application. The TextBox will then take on the look of a TextBox again.


I was going to offer that however if you wanted to build on top of the textbox style then you need that custom style, so I went looking for a way to use the custom style and basing it on the textbox style.
Was This Post Helpful? 0
  • +
  • -

#12 STP_Captain_Slow   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 08-October 10

Re: WPF Custom Control - New TextBox Property

Posted 19 January 2017 - 03:06 AM

Thanks Macey for all your help, funny enough I had seen the pages you posted, the first lost me a bit too. I also wish i scrolled further down and saw your comment iron as i did exactly what you said just before before i saw your comment haha and as you said by taking that line out, pop, up came all the styles, now i understand what that does as you both mention you use that if you want to go crazy and build a style from scratch (i actually knew i was close as i found that "generic" file and hard coded a border colour and thickness and it showed at least the outline).

Again thanks for your help, now to continue on with the basics of WPF after jumping a few levels
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6552
  • View blog
  • Posts: 26,565
  • Joined: 12-December 12

Re: WPF Custom Control - New TextBox Property

Posted 19 January 2017 - 05:16 AM

Quote

I want to create a custom TextBox where i add the Property "IsRequired" (Boolean), and a Trigger to set the TextBox Background Color to White OnEnter.

I'm curious what you intend to do with the IsRequired property? This is not a UI concern, it is to do with the model binding and business rules. WPF has Validation and you can set up a Style and Style Trigger to affect the formatting of a Control and display error notification(s).

Validation in Windows Presentation Foundation

It is also possible to define a Style and Style Trigger to change the background on focus, which could be applied to different types of Controls.

WPF TextBox change color on focus

I don't want to discourage your exploration of Custom Controls, but I'm not sure that one is necessary/appropriate for your requirements.

Control Authoring Overview

Quote

The extensibility of the Windows Presentation Foundation (WPF) control model greatly reduces the need to create a new control. However, in certain cases you may still need to create a custom control. This topic discusses the features that minimize your need to create a custom control and the different control authoring models in Windows Presentation Foundation (WPF).

Of course, once you've spent more time studying WPF, you'll be in a better position to decide what is appropriate.

See also our tutorials section:

WPF & Silverlight Tutorials
Was This Post Helpful? 1
  • +
  • -

#14 STP_Captain_Slow   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 89
  • Joined: 08-October 10

Re: WPF Custom Control - New TextBox Property

Posted 19 January 2017 - 07:41 AM

Thanks Andrew I will work my way though your links, as suggested i have gone back to the basics mainly with the layouts as the XAML is the big new part and i need to understand how things adjust and move on resize etc and how to layout correctly.

In my WinForms version i used the "IsRequired" property to set to true so if the customer tried to save a form and some data was missing it would loop through each textbox control and if IsRequired was True and the Text was "" then flag an error and colour the textbox (this is where the OnEnter = White was used), i thought this was a better way than trying to place validation on every required box.

I'm certainly not the most efficient programmer and am most likely missing a lot of tricks but the programs i write are only for in house use and they seem to work well (even if the code isn't the best) but i'm always keen to learn the "proper way" and everyone loves a new trick!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1