question

StefanSchmidt-6794 avatar image
0 Votes"
StefanSchmidt-6794 asked HuiLiu-MSFT edited

If a custom control with OnApply template really stucks inside the mainwindow constructor function.

Hello,


its always the same ,, i not wanna use custom control so likely,, cause they have one major draw back. that if i wanna expose a part from the template to the application so it can access its properties all seems fine , until one point ,, i can not access that part from the custom control in my application, hosting the dll,, for out the Main constructor method..,, means the dll seems to simply load after this method was called as to get there still an error the object is not set to an instance of an object. What i do wrong there?? Is that really the behavior of a custom control??


here the custom control:
Generic.xaml (only most important things

ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:NumberEnumerateTextBox">
<Style TargetType="{x:Type local:NumberEnumerateTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:NumberEnumerateTextBox}">
<Border >
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ScrollViewer :Name="PART_ContentHost" />
<RepeatButton x:Name="UpCountRepeatButton" Margin="0,2,0,0" Width="9" Height="11"/>
<RepeatButton x:Name="DownCountRepeatButton" Margin="0,0,0,2" Width="Auto" Height="Auto" G />
<Rectangle Opacity="0.5" x:Name="OverlayRectangle" Fill="#FF0F477B"/>
<TextBlock Opacity="0.9" Foreground="White" x:Name="OverlayTextBlock" FontSize="26" Margin="2,-3,0,0" G"/>
</StackPanel >
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

and here is code file from the custom control what i mean to use the OnApply method to assign public members:


public class NumberEnumerateTextBox : Control
{
static NumberEnumerateTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumberEnumerateTextBox), new FrameworkPropertyMetadata(typeof(NumberEnumerateTextBox)));
}

     public RepeatButton UpCountRepeatButton = null;
     public RepeatButton DownCountRepeatButton = null;
     public Rectangle OverlayRectangle = null;
     public TextBlock OverlayTextBlock = null;
     public override void OnApplyTemplate()
     {

         base.OnApplyTemplate();
         UpCountRepeatButton = GetTemplateChild("UpCountRepeatButton") as RepeatButton;
         DownCountRepeatButton = GetTemplateChild("DownCountRepeatButton") as RepeatButton;
         OverlayRectangle = GetTemplateChild("OverlayRectangle") as Rectangle;
         OverlayTextBlock = GetTemplateChild("OverlayTextBlock") as TextBlock;
     }

}

so all seems fine until that point,, but yet i create may solution to test the dll,, also use project reference and add the library to that Solution,,
then i try following in the Main function of the solution,,

also first i assign that control in the xaml code:

xmlns:num="clr-namespace:NumberEnumerateTextBox;assembly=NumberEnumerateTextBox"


<num:NumberEnumerateTextBox x:Name="box" Width="50" Height="50"/>

and then i try in the constructor to call after that object:

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
box.Background = Brushes.Yellow;
}
}


I get there always an error message as the object is still not set to an instance what simply works fine , if ya ahs have loaded already the application and then in a different method ya try again the same access. But me there asking if that behavior is really intention and where could be the mistake to still retrieve such error message?


windows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@StefanSchmidt-6794. Is there any update ? Did my answer solve your problem? If so, you can accept it as the answer. It's helpful for community members with similar questions.

0 Votes 0 ·

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered

You could try to check if your namespace and class names are the same.If it is the same, an error will be reported. You can refer to the code below.
Project structure:

197538-image.png
Generic.xaml:

 <ResourceDictionary
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:local="clr-namespace:NumberEnumerateTextBox">
     <Style TargetType="{x:Type local:NumberEnumerateTextBox1}">
         <Setter Property="Template">
             <Setter.Value>
                 <ControlTemplate TargetType="{x:Type local:NumberEnumerateTextBox1}">
                     <Border Background="{TemplateBinding Background}"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}">
                         <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                             <ScrollViewer x:Name="PART_ContentHost" />
                             <RepeatButton x:Name="UpCountRepeatButton" Margin="0,2,0,0" Width="9" Height="11"/>
                             <RepeatButton x:Name="DownCountRepeatButton" Margin="0,0,0,2" Width="Auto" Height="Auto" />
                             <Rectangle Opacity="0.5" x:Name="OverlayRectangle" Fill="#FF0F477B"/>
                             <TextBlock Opacity="0.9" Foreground="White" x:Name="OverlayTextBlock" FontSize="26" Margin="2,-3,0,0" />
                         </StackPanel >
                     </Border>
                 </ControlTemplate>
             </Setter.Value>
         </Setter>
     </Style>
 </ResourceDictionary>

NumberEnumerateTextBox1:

 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Controls.Primitives;
 using System.Windows.Shapes;
    
 namespace NumberEnumerateTextBox
 {
     public class NumberEnumerateTextBox1 : Control
     {
         static NumberEnumerateTextBox1()
         {
             DefaultStyleKeyProperty.OverrideMetadata(typeof(NumberEnumerateTextBox1), new FrameworkPropertyMetadata(typeof(NumberEnumerateTextBox1)));
         }
     public RepeatButton UpCountRepeatButton = null;
     public RepeatButton DownCountRepeatButton = null;
     public Rectangle OverlayRectangle = null;
     public TextBlock OverlayTextBlock = null;
     public override void OnApplyTemplate()
     {
       base.OnApplyTemplate();
       UpCountRepeatButton = GetTemplateChild("UpCountRepeatButton") as RepeatButton;
       DownCountRepeatButton = GetTemplateChild("DownCountRepeatButton") as RepeatButton;
       OverlayRectangle = GetTemplateChild("OverlayRectangle") as Rectangle;
       OverlayTextBlock = GetTemplateChild("OverlayTextBlock") as TextBlock;
     }
   }
 }

Add NumberEnumerateTextBox dll to OnApplyTemplateCustomControlDemo project.
MainWindow.xaml:

 xmlns:num="clr-namespace:NumberEnumerateTextBox;assembly=NumberEnumerateTextBox"
    <Grid>
         <num:NumberEnumerateTextBox1 x:Name="box" Width="150" Height="150"/>
     </Grid>

MainWindow.xaml.cs:

 public partial class MainWindow : Window
   {
     public MainWindow()
     {
       InitializeComponent();
       box.Background = Brushes.Yellow;
    
     }
   }

The result:
197591-image.png


If the response is helpful, please click "Accept Answer" and upvote it.
 Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 


image.png (10.5 KiB)
image.png (5.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.