I'm trying to create a browser-esque tab interface in WPF. I've got it put together and it worked for a while, but now all the buttons that make the tabs function are disabled. In the live visual tree it says overidden and shows coercion setting IsEnabled = false. None the parent items are disabled.
Couple things I've tried. I tried explicitly stating that everything was enabled. This didn't help. The next thing I tried was to wipe everything out and just use a plain button. This worked, so I think the problem is somewhere in my styles.
This Doesn't Work
<ItemsControl ItemsSource="{Binding PageViewModels}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="70,15,0,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate >
<Button Background="{x:Null}" Height="24" MinWidth="140" Padding="0,0,0,0" BorderThickness="0" Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor} }" CommandParameter="{Binding }">
<Border MinWidth="140" Height="24" >
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource IsEqual}">
<Binding Path=".ID"/>
<Binding Path="DataContext.CurrentPageViewModel.ID" RelativeSource="{RelativeSource AncestorType=Window, Mode=FindAncestor}"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="{StaticResource LIGHT_Main_100}"/>
<Setter Property="BorderThickness" Value="0,0,0,3"/>
<Setter Property="BorderBrush" Value="{StaticResource DARK_CYAN_Secondary_100}"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="Background" Value="{StaticResource LIGHT_Main_85}"/>
<Setter Property="BorderThickness" Value="0,0,.5,.5"/>
<Setter Property="BorderBrush" Value="{StaticResource DARK_Faded }"/>
</Style>
</Border.Style>
<DockPanel VerticalAlignment="Stretch" Margin="10,0,0,0">
<Label DockPanel.Dock="Left" Content="{Binding Name}" FontSize="15" Padding="0" Loaded="Label_Loaded" >
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<DataTrigger Value="False">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource IsEqual}">
<Binding Path=".ID"/>
<Binding Path="DataContext.CurrentPageViewModel.ID" RelativeSource="{RelativeSource AncestorType=Window, Mode=FindAncestor}"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Foreground" Value="{StaticResource DARK_Faded}"/>
<Setter Property="FontWeight" Value="ExtraLight"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</Label.Style>
</Label>
<Button DockPanel.Dock="Right" Background="{x:Null}" Content="⤫" Height="24" Width="28" FontSize="15" Padding="0,-6,0,0" HorizontalAlignment="Right" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" Command="{Binding ClosePage }" CommandParameter="{Binding}">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource IsEqual}">
<Binding Path=".ID"/>
<Binding Path="DataContext.CurrentPageViewModel.ID" RelativeSource="{RelativeSource AncestorType=Window, Mode=FindAncestor}"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="Visibility" Value="Hidden"/>
</Style>
</Button.Style>
</Button>
</DockPanel>
</Border>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
This Does Work
<ItemsControl ItemsSource="{Binding PageViewModels}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="70,15,0,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate >
<Button Content="Test"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
EDIT
This was probably what caused my issues. A while back I copied this code from an example online and didn't understand it, but it worked. I think canExecute was false because I was not sending a string parameter, but rather an object. Thanks for your help @Neil
public ICommand ChangePageCommand
{
get
{
if (_changePageCommand == null)
{
_changePageCommand = new RelayCommand(
p => ChangeViewModel((string) p),
p => p is string);
}
return _changePageCommand;
}
}
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
public bool CanExecute(object parameters)
{
return _canExecute == null ? true : _canExecute(parameters);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameters)
{
_execute(parameters);
}
#endregion // ICommand Members
}
The CanExecute for your ChangePageCommand command is probably returning false. To check simply remove the command binding and see if the button is still disabled.
Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor} }"