FindByName fails with custom ContentControl
Posted: 27 Sep 2022, 11:38
Hello,
I am using a self-made ContentControl with some adjustments (border, images, etc.).
Layouting and Bindings are working fine.
But I struggle to get controls that I place in it by name.
In a parent control, the following call to FindByName gives me null and the actual sizes are 0 when I go manually through the Visual Tree.
It works when I place the Button outside of my content control.
Is there something that I have to look out for or is it a Bug?
xaml:
code behind:
Thanks
I am using a self-made ContentControl with some adjustments (border, images, etc.).
Layouting and Bindings are working fine.
But I struggle to get controls that I place in it by name.
In a parent control, the following call to FindByName gives me null and the actual sizes are 0 when I go manually through the Visual Tree.
It works when I place the Button outside of my content control.
Is there something that I have to look out for or is it a Bug?
Code: Select all
var controlInsideContentControl = (Button)this.FindName("Button1");
var location1 = controlInsideContentControl.TransformToAncestor(controlInsideContentControl.Parent);
var location2 = VisualTreeHelper.GetOffset(controlInsideContentControl);
Code: Select all
<UserControl x:Class="Ui.Controls.BorderPanel.BorderPanelControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:border="clr-namespace:Ui.Controls.BorderPanel"
mc:Ignorable="d"
d:DesignWidth="1920"
d:DesignHeight="1080"
>
<Grid>
<ContentControl Content="{Binding PanelContent, RelativeSource={RelativeSource AncestorType={x:Type border:BorderPanelControl}}}" />
</Grid>
</UserControl>
Code: Select all
#if UNITY_5_3_OR_NEWER
#define NOESIS
using Noesis;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
#endif
namespace Ui.Controls.BorderPanel
{
[ContentProperty(nameof(PanelContent))]
public partial class BorderPanelControl: UserControl
{
public static readonly DependencyProperty PanelContentProperty = DependencyProperty.Register(
nameof(PanelContent), typeof(object), typeof(BorderPanelControl), new PropertyMetadata(default(object)));
public object PanelContent
{
get => this.GetValue(PanelContentProperty);
set => this.SetValue(PanelContentProperty, value);
}
public BorderPanelControl()
{
this.InitializeComponent();
}
#if NOESIS
private void InitializeComponent()
{
NoesisUnity.LoadComponent(this);
}
#endif
};
}