Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite registeredName

Tags:

wpf

storyboard

I've a method that when called returns a LinearGradientBrush with random color for the GradientStop and I use on them mylabel.RegisterName for later use in a storyboard animation. To prevent the error on first call where no registered name are present I do this:

try
{
    myLabel.UnregisterName("GS1");
    myLabel.UnregisterName("GS2");
    myLabel.UnregisterName("GS3");
    myLabel.UnregisterName("GS4");
}
catch
{
}

I have not found a way to overwrite registered name. There's a better way to do this?

like image 951
CB. Avatar asked Aug 24 '26 04:08

CB.


1 Answers

What about UnregisterName?

(That's what I get for attempting to answer when I can only read part of the question...)

Digging around a bit more, may have found another (WAY simpler) alternative:

var isGs1Defined = NameScope.GetNameScope(myLabel).FindName("GS1") == null;

or packaged up nicely:

public static bool IsNameRegistered(DependencyObject depObject, string name)
{
    var namescope = NameScope.GetNameScope(depObject);
    if(namescope == null)
        return false;
    return namescope.FindName(name) != null;
}
like image 84
JerKimball Avatar answered Aug 26 '26 22:08

JerKimball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!