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;
}
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 theItemsControl.ItemContainerGenerator.GetContainerFromItemmethod.