C# WPF:如何从itemscontrol中查找datatemplate中的元素
我有以下问题:应用程序正在使用名为ToolBox的自定义itemscontrol。工具箱的元素称为toolboxitems,是自定义contentcontrol。现在,工具箱存储了许多从数据库检索并显示的图像。为此,我在工具箱控件中使用了一个datatemplate。但是,当我尝试拖放元素时,我得到的不是图像对象,而是数据库组件。我想我应该遍历这个结构来找到图像元素。代码如下: 工具箱:C# WPF:如何从itemscontrol中查找datatemplate中的元素,c#,wpf,data-binding,datatemplate,itemscontrol,C#,Wpf,Data Binding,Datatemplate,Itemscontrol,我有以下问题:应用程序正在使用名为ToolBox的自定义itemscontrol。工具箱的元素称为toolboxitems,是自定义contentcontrol。现在,工具箱存储了许多从数据库检索并显示的图像。为此,我在工具箱控件中使用了一个datatemplate。但是,当我尝试拖放元素时,我得到的不是图像对象,而是数据库组件。我想我应该遍历这个结构来找到图像元素。代码如下: 工具箱: public class Toolbox : ItemsControl { priv
public class Toolbox : ItemsControl
{
private Size defaultItemSize = new Size(65, 65);
public Size DefaultItemSize
{
get { return this.defaultItemSize; }
set { this.defaultItemSize = value; }
}
protected override DependencyObject GetContainerForItemOverride()
{
return new ToolboxItem();
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return (item is ToolboxItem);
}
}
ToolBoxItem:
public class ToolboxItem : ContentControl
{
private Point? dragStartPoint = null;
static ToolboxItem()
{
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(ToolboxItem), new FrameworkPropertyMetadata(typeof(ToolboxItem)));
}
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
base.OnPreviewMouseDown(e);
this.dragStartPoint = new Point?(e.GetPosition(this));
}
public String url { get; private set; }
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.LeftButton != MouseButtonState.Pressed)
{
this.dragStartPoint = null;
}
if (this.dragStartPoint.HasValue)
{
Point position = e.GetPosition(this);
if ((SystemParameters.MinimumHorizontalDragDistance <=
Math.Abs((double)(position.X - this.dragStartPoint.Value.X))) ||
(SystemParameters.MinimumVerticalDragDistance <=
Math.Abs((double)(position.Y - this.dragStartPoint.Value.Y))))
{
string xamlString = XamlWriter.Save(this.Content);
MessageBoxResult result = MessageBox.Show(xamlString);
DataObject dataObject = new DataObject("DESIGNER_ITEM", xamlString);
if (dataObject != null)
{
DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);
}
}
e.Handled = true;
}
}
private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
}
公共类ToolboxItem:ContentControl
{
专用点?dragStartPoint=null;
静态ToolboxItem()
{
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(ToolboxItem)),new FrameworkPropertyMetadata(typeof(ToolboxItem));
}
受保护的覆盖无效OnPreviewMouseDown(鼠标按钮Ventargs e)
{
基于PreviewMouseDown(e);
this.dragStartPoint=新点?(e.GetPosition(this));
}
公共字符串url{get;private set;}
MouseMove上的受保护覆盖无效(MouseEventArgs e)
{
基地移动(e);
如果(例如LeftButton!=鼠标按钮状态已按下)
{
this.dragStartPoint=null;
}
if(this.dragStartPoint.HasValue)
{
点位置=e.GetPosition(本);
如果ToolboxItem的代码隐藏中((SystemParameters.MinimumHorizontalDragDistance)调用类似his的方法:
FindVisualChild<Image>(this);
FindVisualChild(这个);
这对我来说很好,并返回了预期的图像对象。非常感谢,viky。但是datatemplate“在”toolboxm中,而不是toolboxitem。我应该将其添加到工具箱或toolboxitem中吗?我尝试了以下代码:string xamlString=XamlWriter.Save(FindVisualChild(This));现在我得到了一个空图像(没有附加url)。
<Toolbox x:Name="NewLibrary" DefaultItemSize="55,55" ItemsSource="{Binding}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Path=url}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</Toolbox>
FindVisualChild<Image>(this);