前提・実現したいこと
ここに質問の内容を詳しく書いてください。
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/ツールのバージョンなど) ここにより詳細な情報を記載してください。 を

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。