I have a button command that is binded to a RelayCommand AddCommand
whose code is located in my MainViewModel.cs
. The problem I’m facing is the button I need the command to fire in is located in a usercontrol SettingsView.xaml
.
When I bind the command to a button in my MainWindow.xaml
, the ObservableCollection<PanelButton> PanelButton
is udpated properly on click, but once I add the button command to a button in my SettingsView.xaml
the event never fires.
Here is my MainViewModel.cs.
public ObservableCollection<PanelButton> PanelButton { get; set; }
public RelayCommand HomeViewCommand { get; set; }
public RelayCommand SettingsViewCommand { get; set; }
public RelayCommand AddCommand => new RelayCommand(execute => AddNavigationPanel());
public StartingHomeViewModel HomeVM { get; set; }
public SettingsViewModel SettingsVM { get; set; }
private object _currentView;
public object CurrentView
{
get { return _currentView; }
set
{
_currentView = value;
OnPropertyChanged();
}
}
private PanelButton selectedPanelButton;
public PanelButton SelectedPanelButton
{
get { return selectedPanelButton; }
set
{
selectedPanelButton = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
PanelButton = new ObservableCollection<PanelButton>();
panelHandler = new PanelHandler();
HomeVM = new StartingHomeViewModel();
SettingsVM = new SettingsViewModel();
CurrentView = HomeVM;
HomeViewCommand = new RelayCommand(o =>
{
CurrentView = HomeVM;
});
SettingsViewCommand = new RelayCommand(o =>
{
CurrentView = SettingsVM;
});
ShowNavigationPanels();
}
private void ShowNavigationPanels()
{
foreach (string name in panelHandler.PanelNameCollection())
{
PanelButton.Add(new PanelButton
{
Name = name
});
}
}
private void AddNavigationPanel()
{
MessageBox.Show("it works", "success", MessageBoxButton.OK, MessageBoxImage.Exclamation);
panelHandler.CreatePanel("test panel 1");
PanelButton.Add(new PanelButton
{
Name = "test panel 1"
});
}
Here is my MainWindow.xaml
<Window.DataContext>
<viewModel:MainViewModel/>
</Window.DataContext>
<ItemsControl ItemsSource="{Binding PanelButton}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Style="{StaticResource MenuButtonTheme}"
Content="{Binding Name}"
Height="50"
FontSize="14"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ContentControl Grid.Row="1"
Grid.Column="1"
Margin="0"
Padding="5"
Content="{Binding CurrentView}"/>
Here is my SettingsView.xaml
<UserControl.DataContext>
<viewmodel:MainViewModel/>
</UserControl.DataContext>
<Button Background="white"
Height="30"
Width="200"
HorizontalAlignment="Left"
Margin="10,10,0,0"
Content="Create"
Style="{StaticResource RegularButton}"
Command="{Binding AddCommand}"/>
Here is my App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/MenuButtonTheme.xaml"/>
<ResourceDictionary Source="Theme/SearchTheme.xaml"/>
<ResourceDictionary Source="Theme/RegularButtonTheme.xaml"/>
<ResourceDictionary Source="Theme/ScrollbarTheme.xaml"/>
<ResourceDictionary Source="Theme/SettingsTabTheme.xaml"/>
<ResourceDictionary Source="Theme/PanelNavigationTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
<DataTemplate DataType="{x:Type viewModel:StartingHomeViewModel}">
<view:StartingHomeView/>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:SettingsViewModel}">
<view:SettingsView/>
</DataTemplate>
</ResourceDictionary>
</Application.Resources>
EDIT
I added DataContext to SettingsView, which now fires the event which brings another problem. The event fires, prompting the messagebox, but now the ObservableCollection<PanelButton> PanelButton
doesn’t update when fired. Any ideas why this might be happening?