【发布时间】:2010-02-01 16:47:02
【问题描述】:
我有一个类 (MockWI),我在 app.xml 中定义了以下 DataTemplate
<DataTemplate DataType="{x:Type local:MockWI}">
<Button Content="{Binding Name}"/>
</DataTemplate>
在我的代码中,我需要找到 MockWI 实例所具有的 UI 对象。
现在我这样做:
Button elt = new Button { Content = myMockWI};
但这给了我一个按钮中的一个按钮。
我只想获取名为 myMockWI 的 MockWI 按钮。像这样的:
Button elt = GetUIControlFromVar(myMockWI);
有没有办法做到这一点?
添加更多代码以显示上下文:
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;
}
【问题讨论】:
-
您不能直接命名您的
DataTemplate并以这种方式引用它吗? -
好点。那不会给我我真正需要的东西。我已经更改了我的问题以显示我真正需要的内容。
标签: c# wpf data-binding datatemplate