C
C#3y ago
ByLife

.NET WPF APP - Trying to bind a command to a button but not working

57 Replies
ByLife
ByLifeOP3y ago
I'm so confused of why is it not working hhh
Klarth
Klarth3y ago
So this is an error in the XAML editor/designer. You need to be skeptical of those as they're less reliably true than ones coming from C#.
ByLife
ByLifeOP3y ago
Ok ❤️
Patrick
Patrick3y ago
post full xaml
ByLife
ByLifeOP3y ago
Sure
Klarth
Klarth3y ago
Ensure you've tried to build and run the app. If that causes an exception, then it's a real error. If it doesn't actually build, then check the Output window for details.
ByLife
ByLifeOP3y ago
<Window x:Class="WpfApp1.MainWindow"
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:viewModel="clr-namespace:WpfApp1.MVVM.ViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="NasaProject"
Height="600"
Width="920"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
>

<Window.DataContext>
<viewModel:MainViewModel/>
</Window.DataContext>
<Border Background="#272537" CornerRadius="20">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="101*"/>
<RowDefinition Height="499*"/>
</Grid.RowDefinitions>

<TextBlock Text="Nasa Project"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Foreground="White"
FontSize="22"
Margin="20,20,0,0" Height="29" Width="122"
/>

<StackPanel Grid.Row="1">
<RadioButton Content="Home"
Height="50"
Foreground="White"
FontSize="14"
IsChecked="True"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding HomeViewCommand}"
/>

<RadioButton Content="API"
Height="50"
Foreground="White"
FontSize="14"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding AsteroidsViewCommand}"
/>

<RadioButton Content="Home"
Height="50"
Foreground="White"
FontSize="14"
Style="{StaticResource MenuButtonTheme}"
/>
</StackPanel>

<TextBox Width="250"
Height="40"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="5"
Grid.Column="1"
Style="{StaticResource ModernTextbox}" TextChanged="TextBox_TextChanged" Text="fazaf&#xD;&#xA;"
/>

<ContentControl Grid.Row="1"
Grid.Column="1"
Margin="10"
Content="{Binding CurrentView}"/>
</Grid>
</Border>
</Window>
<Window x:Class="WpfApp1.MainWindow"
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:viewModel="clr-namespace:WpfApp1.MVVM.ViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="NasaProject"
Height="600"
Width="920"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
>

<Window.DataContext>
<viewModel:MainViewModel/>
</Window.DataContext>
<Border Background="#272537" CornerRadius="20">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="101*"/>
<RowDefinition Height="499*"/>
</Grid.RowDefinitions>

<TextBlock Text="Nasa Project"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Foreground="White"
FontSize="22"
Margin="20,20,0,0" Height="29" Width="122"
/>

<StackPanel Grid.Row="1">
<RadioButton Content="Home"
Height="50"
Foreground="White"
FontSize="14"
IsChecked="True"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding HomeViewCommand}"
/>

<RadioButton Content="API"
Height="50"
Foreground="White"
FontSize="14"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding AsteroidsViewCommand}"
/>

<RadioButton Content="Home"
Height="50"
Foreground="White"
FontSize="14"
Style="{StaticResource MenuButtonTheme}"
/>
</StackPanel>

<TextBox Width="250"
Height="40"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="5"
Grid.Column="1"
Style="{StaticResource ModernTextbox}" TextChanged="TextBox_TextChanged" Text="fazaf&#xD;&#xA;"
/>

<ContentControl Grid.Row="1"
Grid.Column="1"
Margin="10"
Content="{Binding CurrentView}"/>
</Grid>
</Border>
</Window>
It is building But the buttons won't work
Patrick
Patrick3y ago
are you doing anything in codebehind
ByLife
ByLifeOP3y ago
Let me screen that for you
Patrick
Patrick3y ago
also if you're using MVVM Toolkit I'd just use [RelayCommand] and make a method, to be honest.
ByLife
ByLifeOP3y ago
Thats what I did ^^ In my app.xaml : <Application x:Class="WpfApp1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1" xmlns:viewModel="clr-namespace:WpfApp1.MVVM.ViewModel" xmlns:view="clr-namespace:WpfApp1.MVVM.View" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="./Theme/MenuButtonTheme.xaml" /> <ResourceDictionary Source="./Theme/TextboxTheme.xaml"/> </ResourceDictionary.MergedDictionaries> <DataTemplate DataType="{x:Type viewModel:HomeViewModel}"> <view:HomeView></view:HomeView> </DataTemplate> <DataTemplate DataType="{x:Type viewModel:AsteroidsViewModel}"> <view:AsteroidsView></view:AsteroidsView> </DataTemplate> </ResourceDictionary> </Application.Resources> </Application>
Patrick
Patrick3y ago
that's not what you've posted
ByLife
ByLifeOP3y ago
I'm loading my models Uh Thats how i load it
Patrick
Patrick3y ago
I still need the code behind for the main window
ByLife
ByLifeOP3y ago
Ok It is this one:
using WpfApp1.Core;

namespace WpfApp1.MVVM.ViewModel
{
class MainViewModel : ObservableObject
{

public RelayCommand HomeViewCommand { get; set; }
public RelayCommand AsteroidsViewCommand { get; set; }

public HomeViewModel HomeVM { get; set; }

public AsteroidsViewModel AsteroidsVM { get; set; }

private object _currentView;

public object CurrentView
{
get { return _currentView; }
set
{
_currentView = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
HomeVM = new HomeViewModel();
AsteroidsVM = new AsteroidsViewModel();
CurrentView = HomeVM;

HomeViewCommand = new RelayCommand((o) =>
{
CurrentView = HomeVM;
});

AsteroidsViewCommand = new RelayCommand((o) =>
{
CurrentView = AsteroidsVM;
});
}
}
}
using WpfApp1.Core;

namespace WpfApp1.MVVM.ViewModel
{
class MainViewModel : ObservableObject
{

public RelayCommand HomeViewCommand { get; set; }
public RelayCommand AsteroidsViewCommand { get; set; }

public HomeViewModel HomeVM { get; set; }

public AsteroidsViewModel AsteroidsVM { get; set; }

private object _currentView;

public object CurrentView
{
get { return _currentView; }
set
{
_currentView = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
HomeVM = new HomeViewModel();
AsteroidsVM = new AsteroidsViewModel();
CurrentView = HomeVM;

HomeViewCommand = new RelayCommand((o) =>
{
CurrentView = HomeVM;
});

AsteroidsViewCommand = new RelayCommand((o) =>
{
CurrentView = AsteroidsVM;
});
}
}
}
Patrick
Patrick3y ago
no, that's your view model. I'm asking for the code behind of the main window. MainWindow.xaml.cs
ByLife
ByLifeOP3y ago
Sorry
<Window x:Class="WpfApp1.MainWindow"
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:viewModel="clr-namespace:WpfApp1.MVVM.ViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="NasaProject"
Height="600"
Width="920"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
>

<Window.DataContext>
<viewModel:MainViewModel/>
</Window.DataContext>
<Border Background="#272537" CornerRadius="20">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="101*"/>
<RowDefinition Height="499*"/>
</Grid.RowDefinitions>

<TextBlock Text="Nasa Project"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Foreground="White"
FontSize="22"
Margin="20,20,0,0" Height="29" Width="122"
/>

<StackPanel Grid.Row="1">
<RadioButton Content="Home"
Height="50"
Foreground="White"
FontSize="14"
IsChecked="True"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding HomeViewCommand}"
/>

<RadioButton Content="API"
Height="50"
Foreground="White"
FontSize="14"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding AsteroidsViewCommand}"
/>

<RadioButton Content="Home"
Height="50"
Foreground="White"
FontSize="14"
Style="{StaticResource MenuButtonTheme}"
/>
</StackPanel>

<TextBox Width="250"
Height="40"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="5"
Grid.Column="1"
Style="{StaticResource ModernTextbox}" TextChanged="TextBox_TextChanged" Text="fazaf&#xD;&#xA;"
/>

<ContentControl Grid.Row="1"
Grid.Column="1"
Margin="10"
Content="{Binding CurrentView}"/>
</Grid>
</Border>
</Window>
<Window x:Class="WpfApp1.MainWindow"
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:viewModel="clr-namespace:WpfApp1.MVVM.ViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="NasaProject"
Height="600"
Width="920"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
>

<Window.DataContext>
<viewModel:MainViewModel/>
</Window.DataContext>
<Border Background="#272537" CornerRadius="20">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="101*"/>
<RowDefinition Height="499*"/>
</Grid.RowDefinitions>

<TextBlock Text="Nasa Project"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Foreground="White"
FontSize="22"
Margin="20,20,0,0" Height="29" Width="122"
/>

<StackPanel Grid.Row="1">
<RadioButton Content="Home"
Height="50"
Foreground="White"
FontSize="14"
IsChecked="True"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding HomeViewCommand}"
/>

<RadioButton Content="API"
Height="50"
Foreground="White"
FontSize="14"
Style="{StaticResource MenuButtonTheme}"
Command="{Binding AsteroidsViewCommand}"
/>

<RadioButton Content="Home"
Height="50"
Foreground="White"
FontSize="14"
Style="{StaticResource MenuButtonTheme}"
/>
</StackPanel>

<TextBox Width="250"
Height="40"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="5"
Grid.Column="1"
Style="{StaticResource ModernTextbox}" TextChanged="TextBox_TextChanged" Text="fazaf&#xD;&#xA;"
/>

<ContentControl Grid.Row="1"
Grid.Column="1"
Margin="10"
Content="{Binding CurrentView}"/>
</Grid>
</Border>
</Window>
Patrick
Patrick3y ago
ok, you either deleted the cs file or you're not reading my message properly MainWindow.xaml.cs not MainWindow.xaml
ByLife
ByLifeOP3y ago
using System.Windows; namespace WpfApp1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) { } } }
Patrick
Patrick3y ago
thanks
ByLife
ByLifeOP3y ago
It didnt show up in my file explorer sorry
Patrick
Patrick3y ago
so.... what's not working exactly? seems like it should be working.
ByLife
ByLifeOP3y ago
Sure, the command binding is not working But it is building successfully
Klarth
Klarth3y ago
Without changing too much, I would write your MainViewModel like the following (this won't fix your problem though)
public partial class MainViewModel : ObservableObject
{
public HomeViewModel HomeVM { get; }
public AsteroidsViewModel AsteroidsVM { get; }

[ObservableObject] private ObservableObject _currentView;

public MainViewModel()
{
HomeVM = new HomeViewModel();
AsteroidsVM = new AsteroidsViewModel();
CurrentView = HomeVM;
}

[RelayCommand]
public void HomeView()
{
CurrentView = HomeVM;
}

[RelayCommand]
public void AsteroidView()
{
CurrentView = AsteroidsVM;
}
}
public partial class MainViewModel : ObservableObject
{
public HomeViewModel HomeVM { get; }
public AsteroidsViewModel AsteroidsVM { get; }

[ObservableObject] private ObservableObject _currentView;

public MainViewModel()
{
HomeVM = new HomeViewModel();
AsteroidsVM = new AsteroidsViewModel();
CurrentView = HomeVM;
}

[RelayCommand]
public void HomeView()
{
CurrentView = HomeVM;
}

[RelayCommand]
public void AsteroidView()
{
CurrentView = AsteroidsVM;
}
}
Patrick
Patrick3y ago
define "not working", are you getting an error?
ByLife
ByLifeOP3y ago
It is showing me this error "Object reference not set to an instance of objet "
ByLife
ByLifeOP3y ago
Thanks, I will use it ^^
Klarth
Klarth3y ago
What does your app do at runtime? Is the button disabled? Is it enabled and does nothing when you click it?
Patrick
Patrick3y ago
ok, that's the designer, but does it actually run and execute the command?
ByLife
ByLifeOP3y ago
No it doesn't
Klarth
Klarth3y ago
Ensure you've set a breakpoint in the method/command when testing.
ByLife
ByLifeOP3y ago
Oops sorry I didn't tell you It doesn't even build sorry
ByLife
ByLifeOP3y ago
ByLife
ByLifeOP3y ago
I'm getting this error
Patrick
Patrick3y ago
I would rewrite it the relay commands as per Klarth's suggestion earlier
Klarth
Klarth3y ago
Is this a custom RelayCommand when you're already using Mvvm Toolkit? catsweat
Patrick
Patrick3y ago
it will fix your problem, because the way you initialised the relay command makes it expect a command parameter
ByLife
ByLifeOP3y ago
Ok I'll try ^^
Patrick
Patrick3y ago
(also it's just convenient to make MVVM toolkit do it)
ByLife
ByLifeOP3y ago
Patrick
Patrick3y ago
wait... you're.... not using MVVM Toolkit?
ByLife
ByLifeOP3y ago
I am
Patrick
Patrick3y ago
susge
Klarth
Klarth3y ago
Why do you have ObservableObject and RelayCommand defined? Use the Mvvm Toolkit's versions.
Patrick
Patrick3y ago
ok i think there's confusion here MVVM Toolkit is a Microsoft supported MVVM Framework
ByLife
ByLifeOP3y ago
I mean no I don't but I created my own ... 😂
Klarth
Klarth3y ago
catsweat
Patrick
Patrick3y ago
Yes, you're doing MVVM, but MVVM Toolkit is a separate library with all of this stuff built in ... and also when
ByLife
ByLifeOP3y ago
Ok 😂 I mean I'm beginning in C#
Patrick
Patrick3y ago
CommunityToolkit.Mvvm 8.1.0
This package includes a .NET MVVM library with helpers such as: - ObservableObject: a base class for objects implementing the INotifyPropertyChanged interface. - ObservableRecipient: a base class for observable objects with support for the IMessenger service. - ObservableValidator: a base class for objects implementing the INot...
Patrick
Patrick3y ago
install this and delete your own
ByLife
ByLifeOP3y ago
Ok and how do I add the library globaly in the project ?
Klarth
Klarth3y ago
FYI, this only applies if you're using the Mvvm Toolkit library.
ByLife
ByLifeOP3y ago
Just import it in the mainwindow ?
Patrick
Patrick3y ago
walk before running install via nuget into the WPF project.
ByLife
ByLifeOP3y ago
Ok then thanks, I will inform you if it is actually good ^^ ! ❤️ Thanks a lot Umm yes using the library is much simpler Love y'all Thanks @Klarth ❤️ And you too @superpat64

Did you find this page helpful?