问题
I must confess I have problems understanding the way wpf works. I have a usercontrol BottomControl nested in my Mainwindow. If a Button in BottomControl is clicked I want certain changes in my Mainwindow (changing content of a textbox for example) to occur.
The easy thing to do is obviously to just call a public procedure in the Click_Event but this is not quite elegant. I got so far as to use RoutedCommands.
public static readonly RoutedCommand BottomGridReSize = new RoutedCommand();
In XAML of Usercontrol
<Button Style="{StaticResource TransparentButton}" Command="{x:Static local:Commands.BottomGridReSize}" >
In Code of MainWindow
void BottomGridReSize_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
void BottomGridReSize_Executed(object sender, ExecutedRoutedEventArgs e)
{
\\Do some stuff
}
Obviously Mainwindow can't use these events because ist doesn't recognize them. What am I missing?
Any help would very much be appreciated
Jon
回答1:
just for my understanding: you have a BottomControl with a Button and you when the Button is clicked there should something happen in your mainwindow. so why not simply create a DependencyProperty of type ICommand in your BottomControl and bind this to the Button. if you do this you can simply bind a Command of your MainViewmodel to this DP.
<uc:BottomControl MyDPCommand="{Binding MyMainViewmodelCommand}" />
来源:https://stackoverflow.com/questions/26480279/wpf-routing-usercontrol-command-to-mainwindow