如何创建可以放入内容的用户控件?

时间:2014-04-14 04:50:52

标签: wpf xaml

我试图制作一个自定义的ScrollViewer uc,我想到我不知道如何把东西放在它的标签内。例如

<CustomScrollViewer>
    <This is the place where i want to put things>
</CustomScrollViewer>

是否可以定义一个区域,其中&#34;内部&#34;会有什么东西?

2 个答案:

答案 0 :(得分:0)

您可以这样做:为IEnumerable类型的UserControl创建依赖项属性,并绑定要滚动的ItemsSource。

MainWindow

<Window x:Class="ScrollViewerUserControl.MainWindow"
        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"
        xmlns:this="clr-namespace:ScrollViewerUserControl"
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <x:Array x:Key="ParametersArray" Type="{x:Type sys:String}">
            <sys:String>0</sys:String>
            <sys:String>1</sys:String>
            <sys:String>2</sys:String>
            <sys:String>3</sys:String>
        </x:Array>
    </Window.Resources>

    <Grid>
        <this:CustomScrollViewer Width="100"
                                 Height="30"
                                 ItemsSource="{StaticResource ParametersArray}" />
    </Grid>
</Window>

CustomScrollViewer.xaml

<Grid>
    <ScrollViewer Background="Aquamarine" 
                  Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
                  Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}">

        <ItemsControl ItemsSource="{Binding Path=ItemsSource, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
    </ScrollViewer>         
</Grid>

CustomScrollViewer.xaml.cs

public partial class CustomScrollViewer : UserControl
{
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(CustomScrollViewer));

    public IEnumerable ItemsSource
    {
        get
        {
            return this.GetValue(ItemsSourceProperty) as string;
        }
        set
        {
            this.SetValue(ItemsSourceProperty, value);
        }
    }

    public CustomScrollViewer()
    {
        InitializeComponent();
    }
}

但我认为,在这种情况下,最好像这样创建一个CustomScrollViewer

public class CustomScrollViewer : ScrollViewer 
{
    // Your additional logic here
}

在XAML中使用如下:

<this:CustomScrollViewer Width="100" Height="20">
    <ItemsControl ItemsSource="{StaticResource ParametersArray}" />
</this:CustomScrollViewer>

答案 1 :(得分:0)

你是什么意思&#39;把东西放进去&#39;? 在自定义ScrollViewer的示例中,它将完全按照您使用普通ScrollViewer的方式工作,例如:

<ScrollViewer>
  <DataGrid /> // or whatever controls you want to place within the scrollviewer
</ScrollViewer>

变为

<CustomScrollViewer>
  <DataGrid /> // or whatever controls you want to place within the scrollviewer
</CustomScrollViewer>

对于这种布局控件,它包装了其他控件...所以只需扩展ScrollViewer即可添加你想要的任何更改。

如果您的意思是在CustomScrollViewer上拥有新属性,请遵循Anatoliy关于创建新依赖属性的指导原则,这样您就可以执行以下操作:

<CustomScrollViewer myCustomProperty="WickeyWickeyWhack">
  <DataGrid /> // or whatever controls you want to place within the scrollviewer
</CustomScrollViewer>