使用WPF UserControl创建自定义ScrollViewer

时间:2016-04-08 00:26:52

标签: c# .net wpf

我尝试使用一些自定义项创建ScrollViewer。像这样:

UserControl1.xaml:

<UserControl x:Class="MyApp.Control.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="300">
    <UserControl.Template>
        <ControlTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="20" />
                </Grid.RowDefinitions>
                <ContentPresenter Grid.Row="0" Content="{Binding ElementName=uc, Path=DynamicUserControl}" />
                <Rectangle Grid.Row="1" Fill="#88ff0000" />
            </Grid>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

UserControl1.xaml.cs:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty DynamicUserControlProperty = DependencyProperty.Register("DynamicUserControl", typeof(object), typeof(UserControl1), new PropertyMetadata(null));

    public object DynamicUserControl
    {
        get { return GetValue(DynamicUserControlProperty); }
        set { SetValue(DynamicUserControlProperty, value); }
    }
}

TestForm.xaml(使用UserControl1):

<Window x:Class="MyApp.TestForm"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyApp.Control"
        mc:Ignorable="d"
        Title="TestForm" Height="200" Width="500">
    <Grid Background="{StaticResource AimDarkGradBg01}">
        <local:UserControl1>
            <local:UserControl1.DynamicUserControl>
                <Button>Click me</Button>
            </local:UserControl1.DynamicUserControl>
        </local:UserControl1>
    </Grid>
</Window>

但问题是,无论我在local:UserControl1.DynamicUserControl中添加什么内容,都不会呈现任何内容。

任何人都可以帮助我?

1 个答案:

答案 0 :(得分:1)

问题实际上是你绑定表达式。正确的绑定应该是:

UserControl1.xaml:

<UserControl x:Class="MyControls.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyControls"
             mc:Ignorable="d"
             x:Name="uc">
    <Grid>
        <ScrollViewer>
            <WrapPanel>
                <!-- Dynamic Content -->
                <ContentPresenter Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type control:UserControl1}}, Path=DynamicUserControl}"/>
            </WrapPanel>
        </ScrollViewer>
        <Canvas>
            <!-- Some Code -->
        </Canvas>
    </Grid>
</UserControl>

如果您注意到我删除了您对模板的定义,在这种情况下您不需要它。您只需将代码放在用户控件中即可。

其他文件是正确的。修复我上面告诉你的内容,你很高兴。