0

I am trying to make a column's visibility conditional. If I am running the app as the Admin then the column visibility should set to visible, otherwise it is collapsed. I stepped through my code and I can see my conditions are met. yet, the column I want collapse still appears.

Here is how I have my xaml set up

<UserControl x:Class="Foo.Bar.Views.UserChartView"
    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"
    DataContext="{Binding UserChartViewModel, Source={StaticResource Locator}}"
    mc:Ignorable="d"
    xmlns:local="clr-namespace:Foo.Bar.MyDashboard"
    xmlns:viewmodel="clr-namespace:Foo.Bar.MyDashboard"  
    xmlns:Views="clr-namespace:Foo.Bar.MyDashboard.Views"    
    d:DesignWidth="1920" d:DesignHeight="950">
    <UserControl.Resources>
        <viewmodel:BooleanVisibilityConverter x:Key="BooleanVisibilityConverter"/>        
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <DataGrid x:Name="dgAssignSenarioList"
            ItemsSource="{Binding UserTableCollection}"
            VerticalScrollBarVisibility="Auto"
            AutoGenerateColumns="False"
            HorizontalAlignment="Left"
            VerticalAlignment="Stretch"
            BorderThickness="1"
            CanUserAddRows="False"
            CanUserDeleteRows="False"
            Grid.Row="2"
            SelectionMode="Single"
            IsReadOnly="True" >
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridCell}">
                    <EventSetter Event="UIElement.PreviewMouseLeftButtonDown" Handler="SelectUser" />
                    <Setter Property="Foreground" Value="Black"/>
                    <Setter Property="Background">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource UserHighlightConverterKey}">
                                <Binding Path="IsSelected"/>
                                <Binding Path="IsSelected"/>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>              
                <!--THIS IS THE COLUMN THAT I WANT TO COLLAPSE--> <DataGridTextColumn Header="User ID" Binding="{Binding UserID}" ElementStyle="{DynamicResource DataGridTextColumnWrap}" Visibility="{Binding AdminUtilitiesEnabled, Converter={StaticResource BooleanVisibilityConverter}}" />
                <DataGridTextColumn Width="6*" Header="Username" Binding="{Binding UserName}" ElementStyle="{DynamicResource DataGridTextColumnWrap}" />
                <DataGridTextColumn Width="2*" MinWidth="62" Header="Email" Binding="{Binding Email}" ElementStyle="{DynamicResource DataGridTextColumnWrap}" />       
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

And this is how I have my Viewmodel set up

//my usings

namespace Foo.Bar.Dashboard.ViewModels
{
    public class UserChartViewModel : ViewModelBase //Contains the INotifyPropertyChanged
    {
        #region Properties
        /******************************************************************************************
        * Properties
        *******************************************************************************************/

        //..
        //More Properties declared above


        //The boolean I am using to toggle visibility
        public bool _adminUtilitiesEnabled = false;
        public bool AdminUtilitiesEnabled
        {
            get => _adminUtilitiesEnabled;
            set
            {
                _adminUtilitiesEnabled = value;
                RaisePropertyChanged(nameof(AdminUtilitiesEnabled));
            }
        }
        #endregion Properties


        #region Constructors
        /******************************************************************************************
        * Constructors
        *******************************************************************************************/

        public UserChartViewModel (bool runningAsAdmin)
        {
            //..
            //More initialization code above

            AdminUtilitiesEnabled = runningAsAdmin; //I tested this and I am getting true or false as I tweak
        }

        #endregion Constructors
    }
}

I stepped through this and checked that my code is getting to the boolean converter and it return Collapsed when false. But my column is still visible.

***************************UPDATE********************************************** I also attempted to follow what someone in my current situation tried:

Link

<FrameworkElement x:Name="dummyElement" Visibility="Collapsed"/>
    <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn Header="User ID"
                                Binding="{Binding UserID}"
                                Visibility="{Binding DataContext.IsEnable,
                                          Source={x:Reference dummyElement}}"/>
        <!--Same columns as before-->
        </DataGrid.Columns>
    </DataGrid>

But it's still not working.

What I believe is happening is that the binding is just not working. Can anyone else see what's wrong? Many thanks in advance.

3
  • @mm8 I already tried that source and its still not working for me. thank you. Jul 17, 2019 at 13:30
  • Show us what you have tried then.
    – mm8
    Jul 17, 2019 at 13:31
  • @MM8 See update Jul 17, 2019 at 13:37

0

Browse other questions tagged or ask your own question.