2016-04-08 15 views
0

いくつかのカスタマイズでScrollViewerを作成しようとしています。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:このような

(UserControl1を使用)
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:

<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

問題は実際にバインディング式です。あなたは私はあなたがそれを必要としない場合には、テンプレート、あなたの定義を削除気づいた場合

<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> 

UserControl1.xaml:正しい結合は次のようにする必要があります。あなたは単にあなたのコードをユーザコントロールの中に置くことができます。

他のファイルは正しいです。私があなたに言ったことを修正して、あなたは行くのが良いです。

+0

私はこの例を作成しますが、アプリケーションを実行すると、UserControl1のcontentpresenterは空です。 (私は彼の中に考えを入れます)。 –

+0

@JonnyPiazzi私の答えに追加をご覧ください。 –

+0

もう一度試してみましたが、UserControl内のContentPresenterは完全に空のままです。私は多くのことについていくつかの異なる振る舞いを持つwpf 4を使っていると思っていますが、バージョンがこの動作をしていると思いますか? –

関連する問題