This project is read-only.

Hide close button if you can't close document

May 7, 2012 at 9:26 PM
Edited May 7, 2012 at 9:27 PM

In previous versions, I've re-styled the controls to remove the X and other commands I don't want.  Since that's always felt wrong, and we have nicer Commands + MVVM now, I've hacked in some changes that I'll likely make to the theme when I deploy, unless it becomes standard behavior.

I'll summarize the diffs I made in the mvvmtestapp and the theme to mockup my desired behavior.

Workspace.cs:

 

        private bool CanClose(object parameter)
        {
           if (parameter != null)
           {
              var x = parameter as LayoutDocument;
              if (x != null)
              {
                 var y = x.Content as FileViewModel;
                 if (y != null && string.IsNullOrEmpty(y.TextContent))
                    return false;
              }
              return true;
           }
           return false;
        }

 

FileViewModel.cs: Added Application.Current.Dispatcher.Invoke(new Action(() => CommandManager.InvalidateRequerySuggested())); to the setter for TextContent.  

Note, in my RelayCommand I have a function that works very well for me, which I would probably also add as a local modification to AvalonDock:

 

      public void UpdateCanExecute()
      {
         if (CanExecuteChanged != null)
            CanExecuteChanged(this, new EventArgs());
      }

 

VS2010 theme.xaml:

Changed triggers for the LayoutDocumentTabItem

 

                    <ControlTemplate.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding Path=IsSelected}" Value="true"/>
                                <Condition Binding="{Binding Path=IsEnabled, ElementName=DocumentCloseButton, Mode=OneWay}" Value="true" />
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Visibility" Value="Visible" TargetName="DocumentCloseButton"  />
                            <Setter Property="Source" Value="/AvalonDock.Themes.VS2010;component/Images/PinClose_Dark.png" TargetName="PART_ImgPinClose"  />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding Path=IsActive}" Value="true"/>
                                <Condition Binding="{Binding Path=IsEnabled, ElementName=DocumentCloseButton, Mode=OneWay}" Value="true" />
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Visibility" Value="Visible" TargetName="DocumentCloseButton"  />
                            <Setter Property="Source" Value="/AvalonDock.Themes.VS2010;component/Images/PinClose_Dark.png" TargetName="PART_ImgPinClose"  />
                        </MultiDataTrigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding Path=IsMouseOver, ElementName=bo}" Value="true"/>
                                <Condition Binding="{Binding Path=IsEnabled, ElementName=DocumentCloseButton, Mode=OneWay}"
                                           Value="true" />
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Visibility" Value="Visible" TargetName="DocumentCloseButton"  />
                        </MultiDataTrigger>
                    </ControlTemplate.Triggers>

 

Also added this to the DocumentContextMenu / Close item and LayoutDocumentFloatingWindowControl close button:

Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}"

Thanks

May 7, 2012 at 10:50 PM

Just realized I don't have to change your RelayCommand - I would be using my own in my ViewModel.