C
C#

help

❔ WPF Cannot find child when it's there

IIndeed2/3/2023
Hi! I am working on my own RoundedCorners attachedproperty however I have stumbled upon a problem Problem: When an item that starts invisible goes visible the effect is not applied I've tried experimenting with Loaded and IsVisibleChanged events, where IsVisibleChanged actually fires on the visibility change and the Loaded fires on the start But when IsVisibleChanged fires there are still no children detected in a tree to even try to look for a border
public static class RoundedCorners {
public static readonly DependencyProperty RadiusProperty = DependencyProperty.RegisterAttached(
"Radius",
typeof(CornerRadius),
typeof(RoundedCorners),
new PropertyMetadata(default(CornerRadius), RadiusChanged));

public static void SetRadius(DependencyObject element, CornerRadius value) {
element.SetValue(RadiusProperty, value);
}

public static CornerRadius GetRadius(DependencyObject element) {
return (CornerRadius)element.GetValue(RadiusProperty);
}

public static void RadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
Border? border = d as Border;
if (border == null) {
border = UIHelper.FindChild<Border>(d);
}

if (border == null) {
InitialRadiusSet(d, e);
return;
}

border.CornerRadius = (CornerRadius)e.NewValue;
}

private static void InitialRadiusSet(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var element = d as Control;
if (element == null) return;

element.Loaded -= LoadedHandler;
element.Loaded += LoadedHandler;
}

private static void LoadedHandler(object sender, RoutedEventArgs e) {
var element = sender as Control;
if (element == null) return;

var border = UIHelper.FindChild<Border>(element);
if (border == null) return;

var radius = GetRadius(element);
border.CornerRadius = radius;
}
}
public static class RoundedCorners {
public static readonly DependencyProperty RadiusProperty = DependencyProperty.RegisterAttached(
"Radius",
typeof(CornerRadius),
typeof(RoundedCorners),
new PropertyMetadata(default(CornerRadius), RadiusChanged));

public static void SetRadius(DependencyObject element, CornerRadius value) {
element.SetValue(RadiusProperty, value);
}

public static CornerRadius GetRadius(DependencyObject element) {
return (CornerRadius)element.GetValue(RadiusProperty);
}

public static void RadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
Border? border = d as Border;
if (border == null) {
border = UIHelper.FindChild<Border>(d);
}

if (border == null) {
InitialRadiusSet(d, e);
return;
}

border.CornerRadius = (CornerRadius)e.NewValue;
}

private static void InitialRadiusSet(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var element = d as Control;
if (element == null) return;

element.Loaded -= LoadedHandler;
element.Loaded += LoadedHandler;
}

private static void LoadedHandler(object sender, RoutedEventArgs e) {
var element = sender as Control;
if (element == null) return;

var border = UIHelper.FindChild<Border>(element);
if (border == null) return;

var radius = GetRadius(element);
border.CornerRadius = radius;
}
}
//one of few sample dependency properties
public static readonly DependencyProperty TopLeftProperty = DependencyProperty.RegisterAttached(
"TopLeft",
typeof(double),
typeof(RoundedCorners),
new PropertyMetadata(default(double), TopLeftChanged));

public static void SetTopLeft(DependencyObject element, double value) {
element.SetValue(TopLeftProperty, value);
}

public static double GetTopLeft(DependencyObject element) {
return (double)element.GetValue(TopLeftProperty);
}

private static void TopLeftChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var cornerRadius = GetRadius(d);
cornerRadius.TopLeft = (double)e.NewValue;
SetRadius(d, cornerRadius);
}
//one of few sample dependency properties
public static readonly DependencyProperty TopLeftProperty = DependencyProperty.RegisterAttached(
"TopLeft",
typeof(double),
typeof(RoundedCorners),
new PropertyMetadata(default(double), TopLeftChanged));

public static void SetTopLeft(DependencyObject element, double value) {
element.SetValue(TopLeftProperty, value);
}

public static double GetTopLeft(DependencyObject element) {
return (double)element.GetValue(TopLeftProperty);
}

private static void TopLeftChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var cornerRadius = GetRadius(d);
cornerRadius.TopLeft = (double)e.NewValue;
SetRadius(d, cornerRadius);
}
______ this code handles initial loading in items and then reloading them
private static void InitialRadiusSet(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var element = d as Control;
if (element == null) return;

element.Loaded -= LoadedHandler;
element.Loaded += LoadedHandler;
}

private static void LoadedHandler(object sender, RoutedEventArgs e) {
var element = sender as Control;
if (element == null) return;

var border = UIHelper.FindChild<Border>(element);
if (border == null) return;

var radius = GetRadius(element);
border.CornerRadius = radius;
}
private static void InitialRadiusSet(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var element = d as Control;
if (element == null) return;

element.Loaded -= LoadedHandler;
element.Loaded += LoadedHandler;
}

private static void LoadedHandler(object sender, RoutedEventArgs e) {
var element = sender as Control;
if (element == null) return;

var border = UIHelper.FindChild<Border>(element);
if (border == null) return;

var radius = GetRadius(element);
border.CornerRadius = radius;
}
element.Loaded -= LoadedHandler;
element.Loaded += LoadedHandler;
element.Loaded -= LoadedHandler;
element.Loaded += LoadedHandler;
since in static context we cannot see whether we are subscribed or not _______ offending code:
<Button
Width="40"
Height="40"
Margin="10"
attached:RoundedCorners.Radius="{StaticResource RoundedSmRadius}"
Background="{StaticResource BrushBgOverlay}"
BorderThickness="0"
Command="{Binding ConfirmTitleCommand}">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button
Width="40"
Height="40"
Margin="10"
attached:RoundedCorners.Radius="{StaticResource RoundedSmRadius}"
Background="{StaticResource BrushBgOverlay}"
BorderThickness="0"
Command="{Binding ConfirmTitleCommand}">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
public static T? FindChild<T>(DependencyObject parent)
where T : DependencyObject {
if (parent == null) return null;
T? foundChild = null;

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++) {
var child = VisualTreeHelper.GetChild(parent, i);
T? childType = child as T;
if (childType != null) {
foundChild = (T)child;
break;
}

foundChild = FindChild<T>(child);
if (foundChild != null) break;

}

return foundChild;
}
public static T? FindChild<T>(DependencyObject parent)
where T : DependencyObject {
if (parent == null) return null;
T? foundChild = null;

int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < childrenCount; i++) {
var child = VisualTreeHelper.GetChild(parent, i);
T? childType = child as T;
if (childType != null) {
foundChild = (T)child;
break;
}

foundChild = FindChild<T>(child);
if (foundChild != null) break;

}

return foundChild;
}
child finding method mentioned up
SShinyshark2/3/2023
Items that are invisible or disabled will not participate in events. What are you trying to do specifically? I use a recursive method to find a parent for something in WPF
AAccord2/4/2023
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
❔ HttpClient exiting program during GetStringAsync() and GetAsync()I'm sending a GET to an API, but when debugging, as soon as you step off the last line, it cuts out ✅ Homework Helphttps://i.gyazo.com/f23d758d46581bcfb84f88d4178cd484.png What should I return from the FillArray() ❔ force a ushort value?if i type for example var num = 25L; i am forcing a long value (int64) how can i force a ushort in ❔ Doing post method with entity frameworki'm making a simple API to practice with entity framework but when i try to make a post to a table w❔ I can't reference a class library please helpI can't reference a class library i t says compilation error❔ ASP.NET REMOTE DEVELOPMENTHey does anyone know if I would need nginx for remote development on a ubuntu server from my windowException/NULL/Error handling advice.DeserializeAsync clearly returns a nullable generic object. From the picture we can clearly see that❔ ASP.NET Core web app vs ASP.net web application(.NET Framework)Hi, Ive been working with c# for a while now and would like to make a webapp but I dont know where t✅ IGNORE PEOPLEmy place for testing commands❔ Game Hubhi, how can i program my own game hub, like battle.net where i can download and start my own games ?❔ ✅ beginner class and propertiesi am doing basic program abt class and getting information from input but on the weight section whe❔ Best practice to retrieve MySQL columnsHello wonderful people! Can you please help me understanding what would be the best practice to ret❔ Auto Slide Showhere my code✅ materialDesign:Cardfor some reason it says that closing matrialcard doesent exist and it shows an error❔ Why does this code give the CS8604 warning?I'm confused why the below code gives the CS8604 warning. As far as I am concerned, I am null checki✅ Having trouble adding a picture to my code.what should the path look like?❔ Producer Consumer ISourceBlock<T>, periodically flushing consumed dataHi, I've the following scenario: I've a camera continuously acquiring frames and a c# app receiving ❔ Load async data and display it (WindowsAppSDK / WinUI3)I am currently developing my first windows app with C#, Windows App SDK and WinUI3. I want to do som❔ Best Approach to Implement Authentication and Authorization in Microservices with ASP.NET CoreHi friends, I'm looking for experience share. I have Microservices architecture project, that uses ❔ Performance of many small requests vs less but bigger requestsI have 2 projects: an API hosted on aws and a desktop application. Currently, I'm fetching around 4k