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

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

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

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

XAML

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

Q&A

解決済

1回答

1879閲覧

[UWP]ValidatableBindableBaseを使用して、検証結果のエラーメッセージを表示させたい

zin123

総合スコア11

UWP

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

XAML

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

0グッド

0クリップ

投稿2019/07/16 23:15

前提・実現したいこと

UWP初心者です。
ValidatableBindableBaseを継承したMainPageViewModelのMessageというプロパティにDataAnnotationsの検証用属性をつけて、View側で検証結果をBindして表示させたいです。

該当のソースコード

C#

1// MainPageViewModel.cs 2using Prism.Windows.Validation; 3using System.ComponentModel.DataAnnotations; 4 5namespace ValidatableBindableBaseApp.ViewModels 6{ 7 public class MainPageViewModel : ValidatableBindableBase 8 { 9 private string message; 10 11 [Required(ErrorMessage ="Required")] 12 public string Message 13 { 14 get { return this.message; } 15 set { this.SetProperty(ref this.message, value); } 16 } 17 18 } 19} 20// MainPage.xaml 21<Page 22 x:Class="ValidatableBindableBaseApp.Views.MainPage" 23 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 24 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 25 xmlns:local="using:ValidatableBindableBaseApp.Views" 26 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 27 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 28 xmlns:Mvvm="using:Prism.Windows.Mvvm" 29 Mvvm:ViewModelLocator.AutoWireViewModel="True" 30 mc:Ignorable="d" 31 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 32 33 <StackPanel> 34 <TextBox Text="{x:Bind ViewModel.Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 35 <TextBlock Text="{x:Bind ViewModel.Errors.Errors, Mode=OneWay}"/> 36 </StackPanel> 37</Page> 38 39// MainPage.xaml.cs 40using ValidatableBindableBaseApp.ViewModels; 41using Windows.UI.Xaml.Controls; 42 43// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=234238 を参照してください 44 45namespace ValidatableBindableBaseApp.Views 46{ 47 /// <summary> 48 /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。 49 /// </summary> 50 public sealed partial class MainPage : Page 51 { 52 public MainPageViewModel ViewModel => this.DataContext as MainPageViewModel; 53 public MainPage() 54 { 55 this.InitializeComponent(); 56 } 57 } 58}

###試したこと
ViewModel.Errors.Errorsだと、System.Collections.Generic.Dictionary2[System.String.System.Collections.ObjectModel.ReadOnlyCollection1[System.String]]と表示されます。

補足情報(FW/ツールのバージョンなど)

VisualStudio 2019 community
Prism.Unity 6.3.0

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

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

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

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

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

guest

回答1

0

ベストアンサー

Prism for WinRTのValidatableBindableBase

こちらのサンプルでErrorsからの値取り出しについて説明されてます。

ストアアプリ内での利用方法

このValidatableBindableBaseクラスは、TextBoxなどの入力系コントロールとValidatableBindableBaseを継承したクラスのプロパティをバインドすることが想定されています。そして、Errors[プロパティ名]をTextBlockなどにバインドしてConverterなどを利用して、IReadOnlyCollection<string>から最初の1つを取り出すようにします。コンバータのコード例を以下に示します

実際のコードや使い方はリンク先から確認してください。

今回の例の場合はまずリンク先で説明されている ErrorConverter をプロジェクト内に作成し、その上で Page.Resources に ErrorConverter を配置します。次にTextBlock.Textへのバインディングを MessageプロパティのErrorsを取り出して表示するように修正します。

<Page x:Class="ValidatableBindableBaseApp.Views.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:ValidatableBindableBaseApp.Views" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Mvvm="using:Prism.Windows.Mvvm" xmlns:converters="using:<コンバーターの名前空間>" Mvvm:ViewModelLocator.AutoWireViewModel="True" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <!-- StaticResource から利用できるようにする --> <Page.Rosources> <converters:ErrorConverter x:Key="ErrorConverter" /> </Page.Rosources> <StackPanel> <TextBox Text="{x:Bind ViewModel.Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <!-- MessageプロパティのErrorをバインドする。ErrorConverterによってエラーが一つだけ返される。 --> <TextBlock Text="{x:Bind ViewModel.Errors[Message], Converter={StaticResource ErrorConverter}}" /> </StackPanel> </Page>

また、本件とは別に気になったところがありまして、

Mvvm:ViewModelLocator.AutoWireViewModel="True"としているのに {x:Bind ViewModel.*}を指定するのは、惜しいPrismの使い方かなと思うので、

<Page x:Class="ValidatableBindableBaseApp.Views.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:ValidatableBindableBaseApp.Views" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Mvvm="using:Prism.Windows.Mvvm" xmlns:converters="using:<コンバーターの名前空間>" Mvvm:ViewModelLocator.AutoWireViewModel="True" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Page.Rosources> <converters:ErrorConverter x:Key="ErrorConverter" /> </Page.Rosources> <StackPanel> <!-- ViewModelLocator.AutoWireViewModelによってDataContextにMainPageViewModelが入っているはずなのでBindingをできるはず--> <TextBox Text="{Binding Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <TextBlock Text="{Binding Errors[Message], Converter={StaticResource ErrorConverter}}" /> </StackPanel> </Page>

こうすることでわざわざコードビハインドにViewModelを定義しなくても済むようになるかと思います。

x:Bindは要素のリピートが掛かって大量処理が必要な部分でこそ使うべきと思いますが、ページなど量より複雑さが深くなる場面では単純なBindingの指定にして、Xamlの見通しをよくした状態を保つほうが良いのかなと思います。ご参考までに。

投稿2019/07/16 23:54

tor4kichi

総合スコア763

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

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

zin123

2019/07/21 23:04

返信が遅くなってしまい申し訳ありません。 サンプルを見て実装したところ、Bindingを使う方法では、エラーメッセージを表示させることができました。本当にありがとうございました。 しかし、x:Bindを使用した場合、 Invalid binding path 'ViewModel.Errors[Message]': Syntax error at symbol 'Message' 型'BindableValidator'はコレクションではありません とエラーが出てしまいます。
tor4kichi

2019/07/21 23:32

表示できたようでよかったです。 x:Bindは個人的にあまり使いこんでないので手助けできそうにありませんが、Xaml側ではなくViewModel側でErrors[Message] を解決できれば回避できたりしないかなと。
zin123

2019/07/23 23:32

返信ありがとうございます。 x:Bindについてもう少し調べてみます。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問