Search code examples
c#wpf

Collapse certain TabItem if certain boolean value is true - not working


What I want to achieve

When the c# start up argument has the “IsRegionA” in its 3rd position (delimited with space), then I want to hide certain TabItem in the wpf. However, it somehow does not work.

What I have tried

I have tried many different ways to debug & improve the code, such as:

  1. Check Rider Debug Output and I don’t see any binding error
  2. From the IsRegionA flag in RegionSettings, I checked that from Rider it can refer to the place it is used in xaml
  3. I added more logs. The sequence of the logs during runtime sometimes is:
  • "Start Initalising components"
  • "Finish setting is Region A flag"
  • "Finished Initalising components"

Sometimes it is:

  • "Finish setting is Region A flag"
  • "Start Initalising components"
  • "Finished Initalising components"

Both don't work (Meaning when IsRegionA flag is in the start up argument, the specific TabItem is also not hidden.

Everything seems fine, but it is still not working, but so I do not understand what might be the issue. I confirm that the IsAsia flag is indeed set as True, and I evaluated it with breakpoint.

I used the same way to bind data to show on grid with INotifyPropertyChanged, and it works in the UI. Would this issue relate to the TabItem xaml format/syntax, and not issue with binding?

My Code

MainWindow.xaml

<Window x:Class="MyApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="My App"
        Height="800"
        Width="1294"
        Loaded="Window_Loaded"
        Closed="Window_Closed"
        MouseEnter="Window_MouseEnter"
        Icon="Pics/Monitor.ico">

    <TabItem Name="UnderLyings" Header="Underlyings">
        <TabItem.Style>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Visibility" Value="Visible" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsRegionA}" Value="True">
                        <Setter Property="Visibility" Value="Collapsed" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TabItem.Style>
        <Grid>
            <!-- My grid content here -->
        </Grid>
    </TabItem>
</Window>

MainWindow.xaml.cs:

public MainWindow()
{
    SetRegionFlag();
    EnvironmentAndSettings.init(this);
    Elastic.addLog(message: "Start initializing components");
    InitializeComponent();
    Elastic.addLog(message: "Finished initializing components");
}

private void SetRegionFlag() {
RegionSettings regionSettings = new RegionSettings;
string arg = Environment GetCommandLineArgs()[3];
regionSettings.SetRegionFlag(arg);
}

RegionSetting class:

public class RegionSettings : INotifyPropertyChanged
{
    private static bool _IsRegionA;
    public event PropertyChangedEventHandler PropertyChanged;

    public void SetRegionFlag (string arg)
    {
        if (arg.Equals("IsRegionA"))
        {
            IsRegionA = true;
            Elastic.addLog(message: "Finish setting is Region A flag");
        }
    }

    public bool IsRegionA
    {
        get => _IsRegionA;
        set
        {
            if (HasChanged(_IsRegionA, value))
            {
                _IsRegionA = value;
                NotifyPropertyChanged(nameof(IsRegionA));
            }
        }
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool HasChanged(object oldVal, object newVal)
    {
        return !Equals(oldVal, newVal);
    }
}

Can you please advise what might be the issue / how should I debug it?

Thank you very much in advance.


Solution

  • Seems like the Datacontext is missing.

    You could declare the RegionSettings in the MainWindow class if you need to use some function later. After his creation, set it as Datacontext so that the UI knows that the binding comes trought this class

    public partial class MainWindow : Window
    {
        private RegionSettings regionSettings;
        public MainWindow()
        {
            regionSettings = new RegionSettings();
            this.DataContext = regionSettings;
    
            string arg = "IsRegionA";
            regionSettings.SetRegionFlag(arg);
    
            InitializeComponent();
        }
    }