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

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

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

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

1回答

2141閲覧

Usercontrolにあるボタン(command)を親側でバインドすることは可能でしょうか

hagihagi

総合スコア10

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2018/11/11 11:43

前提・実現したいこと

ここに質問の内容を詳しく書いてください。

WPF(Mvvm)アプリケーションを作成しています。

Usercontrolにあるボタン(command)を親側でバインドすることは可能でしょうか。

処理の流れとしては

1.UCのボタンクリック
2.UCのCommand処理(ucviewmodel)
3.親のCommand処理(mainviewmodel)

といったようなのを考えています。

該当のソースコード

・View <Window x:Class="Sample.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:prism="http://prismlibrary.com/" xmlns:views="clr-namespace:Sample.Views" xmlns:views2="clr-namespace:Sample.Views.Usercontroll" Title="{Binding Title}" Width="525" Height="350" prism:ViewModelLocator.AutoWireViewModel="True"> <Grid> <StackPanel Orientation="Vertical"> <views:UserControlB AddItem="{Binding AddItem}" Text="{Binding Title}" /> </StackPanel> </Grid> </Window> ・viewmodel namespace Sample.ViewModels { public class MainWindowViewModel : ViewModelBase { private DelegateCommand<string> _addItem; public DelegateCommand<string> AddItem => _addItem ?? (_addItem = new DelegateCommand<string>(test)); void test(string parameter) { Debug.Print("デバッグ文を表示 Parent!!!!"); } public MainWindowViewModel(/*IDbContext idb,*/ ITodoRepository rep, IRegionManager regionManager, IEventAggregator eventAggregator) { } } } ---------------- ・view <UserControl x:Class="Sample.Views.UserControlB" 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:local="clr-namespace:Sample.Views" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:prism="http://prismlibrary.com/" d:DesignHeight="450" d:DesignWidth="800" prism:ViewModelLocator.AutoWireViewModel="True" Background="Green" mc:Ignorable="d" Name="root"> <Grid> <StackPanel> <TextBox Text="{Binding Path=Text, ElementName=uc, UpdateSourceTrigger=PropertyChanged}" /> <Button Command="{Binding AddItem, ElementName=root}">Add</Button> </StackPanel> </Grid> </UserControl> .UserControlB.cs public partial class UserControlB : UserControl { public UserControlB() { InitializeComponent(); } public static readonly DependencyProperty AddItemProperty = DependencyProperty.Register( "AddItem", typeof(ICommand), typeof(UserControlB), new UIPropertyMetadata(null)); public ICommand AddItem { get { return (ICommand)GetValue(AddItemProperty); } set { SetValue(AddItemProperty, value); } } } .Viewmodel(UserControl) public class UserControlBViewModel : ViewModelBase { public UserControlBViewModel() { } private DelegateCommand<string> _addItem; public DelegateCommand<string> AddItem => _addItem ?? (_addItem = new DelegateCommand<string>(ExecuteCommandName)); void ExecuteCommandName(string parameter) { Debug.Print("デバッグ文を表示B"); } } ### 試したこと ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。 を

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

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

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

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

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

guest

回答1

0

UserControlBにICommand型の依存関係プロパティを定義し、ボタンのクリックイベントでこのコマンドをExecuteしてみてはいかがでしょうか。これであればMainWindowでUserControlBを記述する際、定義したプロパティにmainviewmodelのコマンドをバインドすることで目的の挙動が達成できるかと思います。

投稿2018/11/14 15:55

Gurz1019_MP

総合スコア196

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問