我创建了一个自定义控件,除其他外,它可以在ItemsControl中显示多个扩展器。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Style TargetType="{x:Type NxTabControl:NxCategoryControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type NxTabControl:NxCategoryControl}">
<ScrollViewer x:Name="contentScrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<StackPanel HorizontalAlignment="Stretch" DockPanel.Dock="Bottom" VerticalAlignment="Stretch" Background="Transparent">
<ItemsControl ItemsSource="{TemplateBinding Content}">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<Border>
<ItemsPresenter/>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</ScrollViewer>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> </ResourceDictionary>
ItemsControl绑定到: ItemsSource =“{TemplateBinding Content} 这是班上的DP:
public class CategoryControl : ItemsControl
{
public ObservableCollection<Expander> Content
{
get { return (ObservableCollection<Expander>)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(ObservableCollection<Expander>), typeof(CategoryControl), new FrameworkPropertyMetadata(new ContentCollection(), FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsParentMeasure)); }
我在用户控件中使用我的自定义控件,方法是设置内容属性并向其添加扩展器:
<NxTabControl:NxCategoryControl x:Name="catControl" DockMode="Top" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Green">
<TabControl:CategoryControl.Content>
<Expander Margin="0 3 0 3" IsExpanded="True" Header="Adresse" >
<ScrollViewer x:Name="ScrollViewerAdresse" HorizontalAlignment="Stretch" ockPanel.Dock="Left">
<Grid Width="{Binding ElementName=ScrollViewerAdresse, Path=ActualWidth, Converter={StaticResource HalfWidthConverter}}">
</Grid>
</ ScrollViewer>
</Expander>
<Expander>
</Expander>
<Expander Header={Binding ExpanderHeader}>
</Expander>
</TabControl:CategoryControl.Content>
我在这种情况下的问题是我无法设置此绑定:
Grid Width="{Binding ElementName=ScrollViewerAdresse, Path=ActualWidth}"
也没有其他绑定到“ElementName”。例如,我也通过其名称和其他元素绑定到XamDataGrids.ActiveRecords。没有任何绑定有效。
Snoop告诉我网格的宽度属性: System.Windows.Data错误:4:找不到引用'ElementName = ScrollViewerAdresse'的绑定源。 BindingExpression:路径= ActualWidth的;的DataItem = NULL;目标元素是'网格'; target属性是'Width'(类型'Double')
我在扩展器中使用的其他绑定,绑定到usercontrol工作的ViewModel,例如:
Header={Binding ExpanderHeader}
欢迎任何帮助! THX!