かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

UWPでUserControlからメインページの画像を非表示にしたい

という感じのフォーラムの質問があった(解決されてたけど)のでGitHubにサンプルあげてみました。 Prism使ってます。

github.com

UserControlへのViewModelの伝搬させてる所がポイントっちゃぁポイントかも。 こんな感じでプロパティをUserControlに用意しておいて

public MainPageViewModel ViewModel
{
    get { return (MainPageViewModel)GetValue(ViewModelProperty); }
    set { SetValue(ViewModelProperty, value); }
}

// Using a DependencyProperty as the backing store for ViewModel.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty ViewModelProperty =
    DependencyProperty.Register("ViewModel", typeof(MainPageViewModel), typeof(SampleUserControl), new PropertyMetadata(null));

MainPageで自分のViewModelを渡す。

<local:SampleUserControl ViewModel="{x:Bind ViewModel, Mode=OneWay}" />