前提・実現したいこと
UWPで以下のようなUserControlを作成したとします。
C#
1using Windows.UI.Xaml; 2using Windows.UI.Xaml.Controls; 3 4namespace TestUWP.Views.Parts 5{ 6 public partial class TestControl : Grid 7 { 8 public readonly DependencyProperty ButtonNameProperty = 9 DependencyProperty.Register( 10 "ButtonName", 11 typeof(string), 12 typeof(TestControl), 13 new PropertyMetadata(default(string))); 14 15 public string ButtonName 16 { 17 get { return (string)GetValue(ButtonNameProperty); } 18 set { SetValue(ButtonNameProperty, value); } 19 } 20 21 public TestControl() 22 { 23 this.InitializeComponent(); 24 } 25 } 26} 27 28 29<Grid 30 x:Class="TestUWP.Views.Parts.TestControl" 31 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 32 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 33 xmlns:local="using:TestUWP.Views.Parts" 34 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 35 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 36 mc:Ignorable="d" 37 x:Name="Test_Control"> 38 39 <Grid.RowDefinitions> 40 <RowDefinition Height="1*"/> 41 <RowDefinition Height="2*"/> 42 </Grid.RowDefinitions> 43 44 <TextBlock Grid.Row="0" 45 Text="テキスト" 46 FontSize="15" 47 TextAlignment="Center"/> 48 49 <Button Grid.Row="1" 50 Content="{Binding ButtonName, ElementName=Test_Control}" 51 FontSize="15" 52 Background="Blue" 53 Foreground="Yellow"/> 54 55</Grid>
上記をPageで使用し、
Pageのxaml上で
ButtonNameを定義すると、
デバッグ時に定義した文字列が表示されました。
C#
1<Page 2 x:Class="TestUWP.Views.Pages.TestPage" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="using:TestUWP.Views.Pages" 6 xmlns:parts="using:TestUWP.Views.Parts" 7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 9 mc:Ignorable="d" 10 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 11 12 <Grid> 13 <Grid.ColumnDefinitions> 14 <ColumnDefinition Width="1*"/> 15 <ColumnDefinition Width="1*"/> 16 </Grid.ColumnDefinitions> 17 <Grid Grid.Column="0"> 18 <parts:TestControl ButtonName="テストインプット"/> 19 </Grid> 20 </Grid> 21 22</Page> 23
次にこのTestControlを継承したクラス
TestControl2を以下のように作成しました。
C#
1namespace TestUWP.Views.Parts 2{ 3 public sealed partial class TestControl2 : TestControl 4 { 5 public TestControl2() 6 { 7 this.InitializeComponent(); 8 } 9 } 10} 11 12<local:TestControl 13 x:Class="TestUWP.Views.Parts.TestControl2" 14 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 15 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 16 xmlns:local="using:TestUWP.Views.Parts" 17 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 18 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 19 mc:Ignorable="d" 20 ButtonName="テストインプット2"> 21 22</local:TestControl> 23
このTestControl2を
TestControlと同様にPageで使用してみたところ
以下のような画面になりました。
C#
1<Page 2 x:Class="TestUWP.Views.Pages.TestPage" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="using:TestUWP.Views.Pages" 6 xmlns:parts="using:TestUWP.Views.Parts" 7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 9 mc:Ignorable="d" 10 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 11 12 <Grid> 13 <Grid.ColumnDefinitions> 14 <ColumnDefinition Width="1*"/> 15 <ColumnDefinition Width="1*"/> 16 </Grid.ColumnDefinitions> 17 <Grid Grid.Column="0"> 18 <parts:TestControl ButtonName="テストインプット"/> 19 </Grid> 20 <Grid Grid.Column="1"> 21 <parts:TestControl2/> 22 </Grid> 23 </Grid> 24 25</Page>
結果は上記のように
TestControl2、ひいては
TestControl2をPageに記述するまでは表示されていた
TestControlのButtonNameをバインドしている表示も消えてしまいました。
今回の疑問は以下の3つになります。
1.TestControlとTestControl2は同様の働きをしてくれるものだと思っていたのですが、
TestControlとTestControl2はどのような違いがあるのでしょうか?
2.なぜTestControlとTestControl2
を両方記述するとTestControlのButtonNameのバインド箇所も表示されなくなってしまったのでしょうか?
3.TestControl2のように独自のコントロールとして作成し
一つ目の画像のような結果をもたらすにはどのように作成すればよろしいでしょうか?
このようなサイトでの質問もまだ慣れておらず、情報不足ありましたら
ご指摘ください。
宜しくお願い致します。
補足情報(FW/ツールのバージョンなど)
・OS バージョン Windows10
あなたの回答
tips
プレビュー