C# 添加到ItemsControl的项不使用ItemTemplate
我对wpf比较陌生,所以我为任何糟糕的编码实践提前道歉 我正在尝试创建一个仪表板应用程序,用户可以通过添加不同的控件(表格、图形等)并移动它们/调整它们的大小来定制它 我最初使用画布来绘制控件,并进行了移动和调整大小操作。由于需要动态的内容,我切换到使用ItemsControl,如下所示:C# 添加到ItemsControl的项不使用ItemTemplate,c#,.net,wpf,data-binding,C#,.net,Wpf,Data Binding,我对wpf比较陌生,所以我为任何糟糕的编码实践提前道歉 我正在尝试创建一个仪表板应用程序,用户可以通过添加不同的控件(表格、图形等)并移动它们/调整它们的大小来定制它 我最初使用画布来绘制控件,并进行了移动和调整大小操作。由于需要动态的内容,我切换到使用ItemsControl,如下所示: <ItemsControl Name="dashboardCanvas" ItemsSource="{Binding Path=CanvasContents}" Grid.Row="1">
<ItemsControl Name="dashboardCanvas" ItemsSource="{Binding Path=CanvasContents}" Grid.Row="1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type local:MoveThumb}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="Control">
<Grid>
<local:ResizeThumb Height="2" Cursor="SizeNS" Margin="0 -4 0 0" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<local:ResizeThumb Width="2" Cursor="SizeWE" Margin="-4 0 0 0" VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
<local:ResizeThumb Width="2" Cursor="SizeWE" Margin="0 0 -4 0" VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
<local:ResizeThumb Height="2" Cursor="SizeNS" Margin="0 0 0 -4" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
<local:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<local:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<local:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
<local:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="DesignerItemTemplate">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<local:MoveThumb Template="{StaticResource MoveThumbTemplate}" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Cursor="SizeAll"/>
<Control Template="{StaticResource ResizeDecoratorTemplate}"/>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
<Button Content="x" VerticalAlignment="Top" HorizontalAlignment="Right" Width="10" Height="10" Click="Button_Click"/>
</Grid>
</ControlTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<local:MoveThumb Template="{StaticResource MoveThumbTemplate}" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Cursor="SizeAll"/>
<Control Template="{StaticResource ResizeDecoratorTemplate}"/>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
<Button Content="x" VerticalAlignment="Top" HorizontalAlignment="Right" Width="10" Height="10" Click="Button_Click"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Top" Value="{Binding Path=Y}"/>
<Setter Property="Canvas.Left" Value="{Binding Path=X}"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
目前,仪表板项仅为:
class DashboardItem : ContentControl
{
}
在代码隐藏中,我有一个仪表板项的ObservableCollection,ItemsControl绑定到该仪表板项
如何强制将ItemsControl模板应用于控件中的所有项?Blinx
我试了一下你的代码,发现了错误
您的代码中存在奇怪的混合。
您不能有ContentControl
的ObservableCollection
。
使您的对象仪表板项目,真正的业务对象(或viewmodel对象)可能包含X和Y属性,但不继承ContentControl
如果您提供GUI对象,WPF似乎不关心您的模板
如果需要为items控件的不同项设置不同的外观,则可以使用
-模板选择器(用于为给定行选择模板)
-或者DataItemTemplate的DataType属性
祝你好运,只是添加了另一个解决方案。正如公认的答案所指出的,如果您直接使用ItemsControl
,您传入的任何GUI对象都将按原样排列在ItemsPanel
上,从而完全绕过DataTemplate
。(如果它不是GUI对象,则首先将其包装在ContentPresenter
中。)
为了解决这个问题,您可以创建ItemsControl
的子类,强制它始终为您的项目创建一个容器。为此,请为IsItemItsOwnContainerOverride返回false
。这告诉控件必须首先将项包装在容器中——同样,在ItemsControl
的默认实现中,容器是一个简单的ContentPresenter
——因此它将始终应用任何给定的ItemTemplate
这是密码
public class EnsureContainerItemsControl : ItemsControl {
protected override bool IsItemItsOwnContainerOverride(object item) {
return false; // Force the control to always generate a container regardless of what was passed in
}
}
我已将DashboardItems更改为仅包含X和Y值(无任何继承),并将其添加到椭圆中(作为占位符)到ItemsControl的DataTemplate中。它现在正在应用模板,尽管移动和调整大小不起作用。我认为这可能与数据上下文有关,但我不这么认为know@Blix,是的,您可以,只是不更改网格的Datacontext:默认情况下,您的DataTemplate具有正确的Datacontext:
。它对我来说很有用。它可以正常显示,但是MoveThumb和ResizeThumb的功能不起作用。MoveThumb和ResizeThumb都是我项目中的类。模板中的控件的datacontext应该是什么?不工作吗?你是说看不见。。。?有什么问题吗?从我在XAML代码中看到的情况来看,在MoveThumb和ResizeThumb中没有使用数据绑定,因此数据绑定/datacontext似乎并不重要
public class EnsureContainerItemsControl : ItemsControl {
protected override bool IsItemItsOwnContainerOverride(object item) {
return false; // Force the control to always generate a container regardless of what was passed in
}
}