簡體   English   中英

使用WPF UserControl創建自定義ScrollViewer

[英]Creating a Custom ScrollViewer using WPF UserControl

我正在嘗試創建一些自定義的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放入什么內容,都不會呈現任何內容。

有人可以幫助我嗎?

問題實際上是您綁定表達式。 正確的綁定應類似於:

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>

如果您注意到我刪除了模板定義,那么在這種情況下就不需要它了。 您只需將代碼放入用戶控件中即可。

其他文件是正確的。 修正我在上面告訴您的內容,您一切順利。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM