0

i'd like to get a control in resource. but it's difficult for me.

1. resource(xaml)

<Span x:Key="spanParagrahTitle" x:Shared="false">
    <InlineUIContainer BaselineAlignment="Center">
        <dgWPFCtrl:IconButton x:Name="ibtnAddToFavorite" Cursor="Hand" IsPressible="True"/>
    <InlineUIContainer BaselineAlignment="Center">
</Span>

2. code

Span myTitle = (Span)appRes["spanParagrahTitle"];
IconButton ibtnAddToFavorite = (IconButton)myTitle.FindName("ibtnAddToFavorite");

How can I get control ibtnAddToFavorite in xaml? of cause, FindName was failed. (It returns null.)

ps. IconButton is User Control.

2 Answers 2

2

Once you have instantiated your Span, you should be able to use VisualTreeHelper to drill down the visual tree and get to your IconButton.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for giving a clue to me. A best way for soling this problem is LogicalTreeHelper.FindLogicalNode() method.
0

From the code that you have provided, I can see dgWPFCtrl:IconButton is not a Resource. Its a Child of InlineUIContainer which in turn is part of resource called "spanParagrahTitle"

So the way to access that is

Span myTitle = (Span)appRes["spanParagrahTitle"]; 
IconButton ibtnAddToFavorite = ((InlineUIContainer)(myTitle.Content)).Content as IconButton;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.