What is the mvvm way of disabling/hiding tabitem's on app load ?

don bradman 626 Reputation points
2022-12-14T09:12:32.247+00:00

I have a WPF app with multiple tabitem's MyTab1, MyTab2, MyTab3 ... etc.

When the app is loaded I want to check the devices MAC address and if there is a mismatch (i.e. it is launched from a different machine) some tabitem's will be hidden.

Currently I have something like:

public partial class Window1 : Window  
    {  
          
        public Window1()  
        {  
            InitializeComponent();  
              
            Window1 window2 = Application.Current.Windows  
                .Cast<Window>()  
                .FirstOrDefault(window => window is Window1) as Window1;  
              
            if (GetMAC() != "99US114229TT")  
            {  
                window2.MyTab2.Visibility=Visibility.Collapsed;  
                window2.MyTab3.Visibility=Visibility.Collapsed;  
            }  
  
        }  
          
        public string GetMAC()  
        {  
            string addr = "";  
            foreach (NetworkInterface n in NetworkInterface.GetAllNetworkInterfaces()) {  
                if (n.OperationalStatus == OperationalStatus.Up) {  
                    addr += n.GetPhysicalAddress().ToString();  
                    break;  
                }  
            }  
            return addr;  
        }  
          
    }  

But how can I do this following MVVM pattern ?

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2022-12-14T11:52:43.997+00:00

    Hi,
    try this demo:

    XAML:

    <Window x:Class="WpfApp1.Window034"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp034"  
            mc:Ignorable="d"  
            Title="Window034" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <TabControl>  
        <TabItem Header="Tab1">  
          <Button Content="Change Visbility of Tab2" Command="{Binding}" Width="200" Height="50"/>  
        </TabItem>  
        <TabItem Header="Tab2" Visibility="{Binding Vis}"/>  
        <TabItem Header="Tab3"/>  
      </TabControl>  
    </Window>  
    

    and ViewModel:

    using System;  
    using System.ComponentModel;  
    using System.Windows;  
    using System.Windows.Input;  
      
    namespace WpfApp034  
    {  
    	public class ViewModel : ICommand, INotifyPropertyChanged  
    	{  
    		public Visibility Vis { get; set; }  
      
    		public event EventHandler? CanExecuteChanged;  
    		public event PropertyChangedEventHandler? PropertyChanged;  
      
    		public bool CanExecute(object? parameter) => true;  
    		public void Execute(object? parameter)  
    		{  
    			this.Vis = (this.Vis == Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible;  
    			PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Vis)));  
    		}  
    	}  
    }  
      
    

    Result:

    270520-x.gif


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.