我是一名新手,对于WPF并不熟悉,因此我不确定问题的标题是否正确或有任何意义,请编辑以使其更相关。我在我的应用程序中使用了Kinect.Toolbox MouseControl。对于使用磁控件,我遇到了一个问题。我知道可以通过在XAML中添加以下内容来定义它们:
<Page ...
xmlns:local ="clr-namespace:Kinect.Toolbox;assembly=Kinect.Toolbox">
...
<Button local:MagneticPropertyHolder.IsMagnetic="True" ... />
....
但我需要在代码中完成。有没有办法在代码中设置磁控件?我可以像这样获取页面上的所有控件:
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
foreach (Button tb in FindVisualChildren<Button>(this))
{
//Set the buttons to be magnetic
}
但我不知道如何通过编程进行设置。