[gelöst][WPF] MVVM, Datenbindung, Command und UserControl
-
Ich habe es bislang hinbekommen, das ich einen Button einer View an ein Command des ViewModels binden kann:
<Window...> ... <Button Command="{Binding FooCommand}" ...> ... </Window>
Hier wurde auch das FooCommand aus dem ViewModel (gebunden über den DataContext) aufgerufen.
Nun würde ich gerne ein UserControl erstellen, das ebenso eine Command-Property nach außen gibt, das ich folgendes schreiben kann:
<Window...> ... <ctrl:MeinControl Command="{Binding FooCommand}" ...> ... </Window>
Ich habe es aber bislang nicht geschafft eine solche Bindung zu erreichen. Mein erster Versuch (Des UserControls) sieht wie folgt aus:
//... public partial class MyControl { //... public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(MyControl)); public ICommand Command { get { return (ICommand)this.GetValue(CommandProperty); } set { this.SetValue(CommandProperty, value); } } //... }
<UserControl x:Class="...MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" DataContext="{Binding RelativeSource={RelativeSource self}}"> ... <Button Command="{Binding Path=Command}" .../> </UserControl>
Nachtrag:
Nachdem ich den DataContext entfernt habe, dem Control einem Namen (x:Name="control") gegeben habe, und beim Binding "ElementName=control" ergänzt habe funktioniert es nun.
-
Was ist genau dein Problem? (Außer das du im letzten Codeblock den "Button" verwendet hast)
-
David W schrieb:
Was ist genau dein Problem? (Außer das du im letzten Codeblock den "Button" verwendet hast)
Innerhalb des UserControl verwende ich einen Button, dessen Command ich nach außen als die Command-Eigenschaft des UserControls geben möchte. Die Bindung klappt aber nicht (Sprich: Das Ereignis wird beim Click nicht ausgelöst).
-
Hattest du nicht mal nach Bindingfehlern im Visual Studio Output geschaut?
Dann hättest du früher gemerkt das du den DataContext durch das Binding überschrieben hast, dadurch sucht er den FooCommand nicht im DataContext des Parents.Das Problem kannst du nur umgehen indem du im UserControl den DataContext _nicht_ setzt.
Da es eh nicht MVVM ist kannst du dich im Code direkt an den Click hängen.
<Window...> ... <Button Command="{Binding FooCommand}" ...> ... </Window>
<Window...> ... <ctrl:MeinControl Command="{Binding FooCommand}" ...> ... </Window>
//... public partial class MyControl { //... public MyControl() { InitializeComponent(); FooButton.Click += HandleFooButtonClick; } private void HandleFooButtonClick(object sender, RoutedEventArgs e) { if (Command != null) Command.Execute(null); } public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(MyControl)); public ICommand Command { get { return (ICommand)this.GetValue(CommandProperty); } set { this.SetValue(CommandProperty, value); } } //... }
<UserControl x:Class="...MyControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> ... <Button Command="{Binding Path=Command}" .../> </UserControl>