-1

I'm using WPF with a TabControl. I've defined an ItemTemplate that sets a TextBlock for the title and a button to close the tab. I would like for the first tab to not have the close button visible. I've tried to set Visiblity of the button using Binding to a Public bool in my ViewModel and convert using the BooleanToVisibilityConverter but it looks like the ItemTemplate is being rendered when the TabControl is created and not when each Tab is added.

Here's my TabControl xaml (I've tried with and without the FallbackValue)

<TabControl x:Name="Items" Grid.Row="1" Grid.Column="0">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding DisplayName}" />
                    <Button x:Name="CloseTab" Content="X" Visibility="{Binding IsTabCloseButtonVisible, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Hidden}"
                            cal:Message.Attach="DeactivateItem($dataContext, 'true')" />
                </StackPanel>
            </DataTemplate>
        </TabControl.ItemTemplate>
    </TabControl>

In App.xaml I have the converter

    <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <local:Bootstrapper x:Key="Bootstrapper"/>
            </ResourceDictionary>
         </ResourceDictionary.MergedDictionaries>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </ResourceDictionary>
</Application.Resources>

And in the ViewModel I have a public property

private bool _isTabCloseButtonVisible;

    public bool IsTabCloseButtonVisible
    {
        get { return _isTabCloseButtonVisible; }
        set 
        {
            _isTabCloseButtonVisible = value;
            NotifyOfPropertyChange(() => IsTabCloseButtonVisible);
        }
    }

I've tested that the property is being set by binding a Textblock to the IsTabCloseButtonVisible and it's changes from false to true.

Is it possible to acheive what I'm trying to do?

RaKer
  • 279
  • 1
  • 5
  • 18
  • I'm not sure what is happening in your code. But everything works fine using the code you provided. – Keithernet Jun 04 '20 at 11:24
  • @RaKer: Where exactly is the `IsTabCloseButtonVisible` defined? And what is the `ItemsSource` of the `TabControl`? – mm8 Jun 04 '20 at 13:05
  • @mm8 the property is defined in the ViewModel associated with the view and the TabControl is bound to an ObservableCollection os ViewModels (I'm using Caliburn Micro for the MVVM framework) – RaKer Jun 04 '20 at 16:11

1 Answers1

0

I've managed to fix the problem. I had the property in the wrong ViewModel so it was not being bound.

RaKer
  • 279
  • 1
  • 5
  • 18