从数据对象获取UI对象 [英] Get the UI object from a data object
问题描述
< DataTemplate DataType = {x:Type local:MockWI}>
< Button Content ={Binding Name}/>
< / DataTemplate>
在我的代码中,我需要找到一个MockWI实例的UI对象。
现在我这样做:
按钮elt = new Button {Content = myMockWI };
但是这给了我一个按钮的按钮。
我想要获取名为myMockWI的MockWI按钮。这样的东西:
按钮elt = GetUIControlFromVar(myMockWI);
有没有办法这样做?
< hr>
添加更多代码以显示上下文:
public UIElement GetVisualFeedback(IDataObject obj )
{
MockWI test = ExtractElement(obj);
//由于内容设置为MockWI,我在按钮中找到一个按钮。
按钮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;
}
没有这样的方法,如果只是因为数据对象和UI对象之间没有一对一的关系,也就是说可能有几个UI对象,数据上下文指向同一数据对象。
如果你的数据对象是某些项目控件的一部分( ItemsControl
, ListBox
, ListView
, DataGrid
等),您可以使用 ItemsControl.ItemContainerGenerator获取相关的项目容器。 GetContainerFromItem
方法。
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 the ItemsControl.ItemContainerGenerator.GetContainerFromItem
method.
这篇关于从数据对象获取UI对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!