CustomControl woes

I spent all day trying to get a very simple CustomControl working. All I wanted it to do was to display a Button.

To do so, I followed the Case Study by Kevin Moore ( http://winfx.msdn.microsoft.com/library/default.asp url=/library/en-us/wcp_conceptual/html/ecb97ed4-e659-48a4-a1e0-893309569f2f.asp ) wherein he shows how to implement a Color Picker Control.

At one point in the study there is the sentence : "For now we will just put in a TextBlock to make sure things are working correctly." ... I did exactly that (not even bothering with a Button). I built a Avalon Application to test the Control ... but lo, the TextBlock was not displayed.

To investigate where I might have missed something I downloaded the complete sample and run it.
Despite the generic.xaml having Textboxes defined none of them are displayed ...

http://img.photobucket.com/albums/v228/shamela/colorpicker.jpg

... so I basically faced a sample that apparently had the same issues that I have.

If this all doesn't make sense ... well, to be honest, it doesn't make any sense to me either.



Answer this question

CustomControl woes

  • Ludwig

    Thanks for your patience. I was out of town.

    Please download and build this solution: http://j832.com/work/ExternalControlLibrary.zip

    I've updated ColorPicker to be in an external assembly. If this solution works for you, we know its an issue with your project, which we can dig into.

    The one thing I neglected to mention is to use ComponentResourceKey if you are creating any resources in your theme file(s).

  • Andrei P

    Yup, the control is within a seperate library ... and it compiles perfectly fine too :)
    The problem is only, nothing is displayed.

    Hmm, let me re-iterate what I did ...
    1. Using VS 2005 Beta2 I created a project of the type Control Library
    2. Within this project I added a Custom Control (myCustomControl)
    3. In the generated generic.xaml I added a Textbox between the ControlTemplate tag:

    <Style TargetType="{x:Type local:MyCustomControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:MyCustomControl}">
                        <TextBlock Text="Hello Mars" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

     

    4. In my Tester Application I add a reference the Control Library.
    5. Within my applications main form I map the namespace and assembly of the control library and use the control

    < Mapping XmlNamespace="controls" ClrNamespace="MyControlLibrary" Assembly="MyControlLibrary" >
    <Window  ....  xmlns:cc="controls" ...>
        <StackPanel>
            <cc:MyCustomControl />
        </StackPanel>
    </Window>

     

    6. The whole solution compiles, the window - however - remains empty :(

  • Lawrence Parker

    You are a star, Kevin.


  • Johan Zietsman

    Hi there, is there any way to create a custom control by the code force only (no XAML)
  • Connie Daniel

     Kevin Moore wrote:

    Look at the ColorPicker case study (http://winfx.msdn.microsoft.com/library/default.asp url=/library/en-us/wcp_conceptual/html/ecb97ed4-e659-48a4-a1e0-893309569f2f.asp)

    Under "Project-Specific Content to Add"

    Do you define MarkupResources in your project file
    Do you define the assembly:ThemeInfo attribute
    Do you set the DefaultStyleKeyProperty in your classes static constructor



    Thanks for your answer Kevin,

    this case study is exactly what I 'use' to teach myself Custom Control authoring. As I pointed out above, however, even that case study has the issue of Textboxes not being displayed. See this screenshot ( http://img.photobucket.com/albums/v228/shamela/colorpicker.jpg ) and compare to how the control displays on your screen, I cannot get rid of the feeling that there are three textboxes containing the r,g, and b values are supposed to be visible.

    As to the three questions.

    Do you define MarkupResources in your project file
    I expected the VS Avalon Custom Control Template to do that for me ... double checked and comparing my project file with that of the Case Study I noticed the MarkupResource definition is missing in my project file.

    However, manually adding the <MarkupResource> to the project file leads to a compile error: Error    3    Unknown build error, 'Item has already been added. Key in dictionary: 'themes/generic.baml'  Key being added: 'themes/generic.baml''
    This most probably is due to VS already adding <Page Include="themes\generic.xaml" /> to the project file. I dumped this in favour of the  <MarkupResource>. Consequently, the compile error did not appear again ... yet, the window still remained blank :(

    Do you define the assembly:ThemeInfo attribute
    Yes. The template does that.

    Do you set the DefaultStyleKeyProperty in your classes static constructor
    Yes. The template does that.

    I am a 'bit' at a loss here, for the love of god I cannot figure out what I am doing wrong ... 2356 grey hairs and counting ...



  • Bas van Atteveldt

    Hi Kevin, overriding of OnRender is definitely not the way I'd like to go. Could you please provide with a simple example (event 3 buttons will suffice) of how to create a proper 'composite' control.

    By the way - would CIder support those controls e.g. will I be able to see check the look at design time

  • delasare

    Kevin, where in the .csproj file do we add the <MarkupResource> What exactly does it do The documentation for it says that it's an internal class and we should never use it.

  • Gary Strader

    Yes.

    You can compose elements directly in code or simply override OnRender.

    We recommend that you subclass from FrameworkElement if you do these things.

    Subclassing from Control (or one of its baseclasses) implies that you want to support custom templates. These templates are defined in XAML.

  • Jamie Sellars

     jedediah wrote:
    Kevin, where in the .csproj file do we add the <MarkupResource> What exactly does it do The documentation for it says that it's an internal class and we should never use it.

    In the Color Picker Case Study the <MarkupResource> is placed within the same <ItemGroup> the <Compile> tags can be found.

  • mic90264

    should the target type be cc:mycustomcontrol

  • Sazid Khan

    Let's see if you we get your straightened out.

    First, are you building the control in an external library (dll)

    Are you able to get the control working if you create it in the same assembly as your application

  • Hema Bairavan

    Look at the ColorPicker case study (http://winfx.msdn.microsoft.com/library/default.asp url=/library/en-us/wcp_conceptual/html/ecb97ed4-e659-48a4-a1e0-893309569f2f.asp)

    Under "Project-Specific Content to Add"

    Do you define MarkupResources in your project file
    Do you define the assembly:ThemeInfo attribute
    Do you set the DefaultStyleKeyProperty in your classes static constructor



  • marius bogdan

    I start to be pretty sure about the whole thing being a culture issue.

    I desperately tried to at least get a UserControl running. As long as I have the UserControl within the same assembly as the Avalon Application, things run (more or less) fine (more or less because I don't seem to to able to convince my solution to accept more than one UserControl ... but I guess that is another issue).

    When I put the UserControl to another project I get an exception : "Cannot locate the resource 'myapp.baml'" ... I am pretty used to that exception, and sometimes changing the the culture settings in AssemblyInfo.cs helps ... sometimes.

    Well, so manually added a <UICulture>de-CH</UICulture> to my project file (I am swiss german and so is my winxp setup) and changed the AssemblyInfo.cs accordingly to contain the line [assembly: NeutralResourcesLanguage("de-CH", UltimateResourceFallbackLocation.Satellite)]

    To no avail ... 14 hours of trying to create a simple control ... not a single line of working code ... I am pretty sure I grew quite a bit of grey hair today.

  • KTamas

    Here's a simple sample of composing two buttons in code.

    public class DoubleButton : FrameworkElement
    {
       
    public DoubleButton()
       {
          _button1 =
    new Button();
          _button2 =
    new Button();
          VisualOperations.GetChildren(this).Add(_button1);
          VisualOperations.GetChildren(this).Add(_button2);
       }

       protected override Size MeasureOverride(Size availableSize)
       {
          Size halfSize = new Size(availableSize.Width,availableSize.Height/2);
          _button1.Measure(halfSize);
          _button2.Measure(halfSize);

          return
    new Size(Math.Max(_button1.DesiredSize.Width,
             _button2.DesiredSize.Width),
             _button1.DesiredSize.Height + _button2.DesiredSize.Height);
       }

       protected override Size ArrangeOverride(Size finalSize)
       {
          _button1.Arrange(
    new Rect(0, 0, finalSize.Width, finalSize.Height / 2));
          _button2.Arrange(
    new Rect(0, finalSize.Height/2, finalSize.Width, finalSize.Height / 2));
          return finalSize;
       }
       private Button _button1, _button2;
    }


    For a full sample, take a look at my latest blog post (http://blogs.msdn.com/okoboji/archive/2005/10/22/483854.aspx). I have a MineFieldElement that derives from FrameworkElement and composes a set of ContentControls.

    As far as tool support, Cider/Sparkle will be able to show these controls and let you compose them in an application.

    If you want to design a control with hard-wired visuals (not using templates) you can use UserControl, which is similiar to UserControl in WinForms.

  • CustomControl woes