views:

49

answers:

2

I have a class (MockWI) that I have defined the following DataTemplate in app.xml

<DataTemplate DataType="{x:Type local:MockWI}">
    <Button Content="{Binding Name}"/>
</DataTemplate>

In my code I need to find the UI object that an instance of MockWI has.

Right now I do this:

Button elt = new Button { Content = myMockWI};

But that gives me a button in a button.

I want to just get the button that is the MockWI called myMockWI. Something like this:

Button elt = GetUIControlFromVar(myMockWI);

Is there a way to do this?


Adding More Code to show Context:

    public UIElement GetVisualFeedback(IDataObject obj)
    {
        MockWI test = ExtractElement(obj);

        // Since Content is set to a MockWI I get a button in a button.
        Button elt = new Button{ Content = test, Opacity = 0.5, IsHitTestVisible = false };

        DoubleAnimation anim = new DoubleAnimation(0.75, new Duration(TimeSpan.FromMilliseconds(500)))
                                   {
                                       From = 0.25,
                                       AutoReverse = true,
                                       RepeatBehavior = RepeatBehavior.Forever
                                   };
        elt.BeginAnimation(UIElement.OpacityProperty, anim);

        return elt;
    }
+1  A: 

There's no such method, if only for the reason that there is not a one-to-one relationship between data objects and UI objects, i.e. there could be several UI objects with data contexts pointing at the same data object.

If your data object is part of some kind of items control (ItemsControl, ListBox, ListView, DataGrid, etc.) you can obtain the relevant item container by using the ItemsControl.ItemContainerGenerator.GetContainerFromItem method.

Aviad P.
But I am not looking for databound objects. I am looking for the UI object represented made by the DataTemplate for myMockWI. If I have a MockWI on the screen it should be displayed as my DataTemplate. 1 to 1 right? Shouldn't I be able to get this value?
Vaccano
I added more code to show what I am trying to do. Maybe you have a suggestion or two?
Vaccano
Your data object can be represented multiple times on your UI. Everywhere you have a `<ContentControl ...>` with content bound to your data object, will have an instance of the data template representing it on the UI.
Aviad P.
Ah, I see. Well dang. I guess I will have to find another way.
Vaccano
+1  A: 

Well, you shouldn't do anything like that (there's for sure a more correct way architecturally).

If you need to do animation to something inside of the DataTemplate why don't you use EventTrigger-s or simple triggers with EnterActions/ExitActions where you can specify which animation to run.

The animation (Storyboard) can be written in XAML and/or designed using a tool (e.g. Blend).

arconaut
I am not doing the animation inside the data template. I just want to know what it looks like. (The animation is really a drag and drop animation for while it is dragging.
Vaccano