Button isn't disabled when command can't be fired

P

pfinferno

Guest
I have a ListBox bound to an ObservableCollection of objects in my viewmodel. I bound the SelectedItem of the ListBox to a property in my viewmodel. I have a bool that checks if the SelectedItem is null, and two Commands that can execute only if that bool isn't null.

I have two viewmodels set up exactly the same way using the above. On one, it works properly, and the two Buttons that use the Commands are properly disabled when the SelectedItem is null. With the other viewmodel, even when the SelectedItem is null, the Buttons aren't disabled. But yet, clicking on them does NOT fire the command. I have no idea why this is happening in one viewmodel, but not the other.

Here's the ViewModel that doesn't work:

public ObservableCollection<PolyStyle> PolyStyles { get; set; }

private PolyStyle _selectedPolyStyle;
public PolyStyle SelectedPolyStyle
{
get => _selectedPolyStyle;
set
{
if (_selectedPolyStyle != value)
{
_selectedPolyStyle = value;
if (value == null)
{
return;
}
else
{
//set some other properties in the viewmodel
}
OnPropertyChanged();
}
}
}

public bool CanModifyOrDelete() { return SelectedPolyStyle != null; }

public RelayCommand ModifyPolyStyleCmd { get; set; }
public RelayCommand DeletePolyStyleCmd { get; set; }

public PolyStylesViewModel()
{
PolyStyles = GetPolyStyles();

SelectedPolyStyle = PolyStyles.First();

ModifyPolyStyleCmd = new RelayCommand(ModifyPolyStyle, CanModifyOrDelete);
DeletePolyStyleCmd = new RelayCommand(DeletePolyStyle, CanModifyOrDelete);
}

private void ModifyPolyStyle()
{
//save off new style
SelectedPolyStyle = PolyStyles.Last();
}


private void DeletePolyStyle()
{
PolyStyles.Remove(SelectedPolyStyle);
}


Here's the relevant parts of the view for the above viewmodel:

<ListBox ItemsSource="{Binding PolyStyles}" SelectedItem="{Binding SelectedPolyStyle, Mode=TwoWay}" DisplayMemberPath="Name"/>
<Button Command="{Binding ModifyPolyStyleCmd}"/>
<Button Command="{Binding DeletePolyStyleCmd}"/>


Next, here's the viewmodel that does work. Notice how it's exactly the same:

public ObservableCollection<PointStyle> PointStyles { get; set; }

private PointStyle _selectedPointStyle;
public PointStyle SelectedPointStyle
{
get => _selectedPointStyle;
set
{
if (_selectedPointStyle != value)
{
_selectedPointStyle = value;
if (value == null)
{
return;
}
else
{
//set some other properties in the viewmodel
}
OnPropertyChanged();
}
}
}

public bool CanModifyOrDelete() { return SelectedPointStyle != null; }

public RelayCommand ModifyPointStyleCmd { get; set; }
public RelayCommand DeletePointStyleCmd { get; set; }

public PointStylesViewModel()
{
PointStyles = GetPointStyles();

SelectedPointStyle = PointStyles.First();

ModifyPointStyleCmd = new RelayCommand(ModifyPointStyle, CanModifyOrDelete);
DeletePointStyleCmd = new RelayCommand(DeletePointStyle, CanModifyOrDelete);
}

private void ModifyPointStyle()
{
//save off new style
SelectedPointStyle = PointStyles.Last();
}


private void DeletePointStyle()
{
PointStyles.Remove(SelectedPointStyle);
}


Here's the relevant parts of the view for the above viewmodel:

<ListBox ItemsSource="{Binding PointStyles}" SelectedItem="{Binding SelectedPointStyle, Mode=TwoWay}" DisplayMemberPath="Name"/>
<Button Command="{Binding ModifyPointStyleCmd}"/>
<Button Command="{Binding DeletePointStyleCmd}"/>


Both of these views are on separate tabs inside of a tabcontrol, on the same window. I noticed that when I switch to a different tab and then back to the PolyStyles tab, the buttons seem to refresh and are in the correct state. None of the others do this though.

Continue reading...
 
Top