1

Ok, so I know that there are a lot of questions like this, but none of them seem to help me.

So I have a property that I wan't to use to set the visibility of a TabItem (so I'm not interested in updates of the property).

The problem is just that the Binding doesn't work and I'm not sure why? The VS output doesn't give me any clues.

Anyway, here's a code sample of the XAML:

<Window x:Class="WpfTestApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis" />
    </Window.Resources>
    <Grid>
        <TabControl>
            <TabItem Header="tabItem1" />
            <TabItem Header="Hide me!" Visibility="{Binding ShowTab, Converter={StaticResource BoolToVis}}" />
        </TabControl>
    </Grid>
</Window>

And here's the .cs

    public bool ShowTab { get; set; }

    public MainWindow()
    {
        ShowTab = false;
        InitializeComponent();
    }

What am I missing? is there supposed to be some kind of DataContext connection somewhere? or is the code some kind of static resource? And why doesn't I get any clue from VisualStudio?

2
  • where is BoolToVis Converter defined, and where is _loadUserGroups defined ? Commented Oct 18, 2012 at 10:40
  • BoolToVis is defined in the resources, _loadUserGroups was probably a typo, he edited it ;) Commented Oct 18, 2012 at 10:45

1 Answer 1

2

Add DataContext = this; to your MainWindow constructor

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        ShowTab = false;
    }

Please notice that your UI will not receive any notification if you modify ShowTab.

Sign up to request clarification or add additional context in comments.

1 Comment

In VS -> Options -> Debugging -> Output Window -> WPF Trace Settings, if you have everything on Warning level at least, VS will tell you about missing bindings instead of quietly ignoring it (which is the default). Very helpful for missing / mismatched data contexts and bindings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.