質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
UWP

UWPは、Universal Windows Platformの略。様々なデバイス向けに提供されているアプリケーションを共通のフレームワーク上で動作可能にする仕組みで、Windows10で導入されました。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

Q&A

0回答

1170閲覧

ユーザーコントロールからさらに独自のコントロールのクラスを継承で作成した際、DependencyPropertyのバインド部分が表示されない?

hirotamasami

総合スコア5

UWP

UWPは、Universal Windows Platformの略。様々なデバイス向けに提供されているアプリケーションを共通のフレームワーク上で動作可能にする仕組みで、Windows10で導入されました。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

0グッド

0クリップ

投稿2020/10/26 08:16

前提・実現したいこと

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問