WPF DataGRidTextColumn绑定可见性不起作用

时间:2019-07-17 13:13:12

标签: wpf

我正在尝试使列的可见性成为条件。如果我以管理员身份运行该应用程序,则列可见性应设置为可见,否则为折叠。我单步执行代码,可以看到符合条件。但是,仍然要显示我要折叠的列。

这是我设置xaml的方式

<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>

这就是我设置Viewmodel的方式

//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
    }
}

我逐步完成了这一步,并检查我的代码是否已到达布尔转换器,并且在返回false时返回Collapsed。但是我的专栏仍然可见。

***************************更新******************* **************************** 我还尝试遵循当前情况下有人尝试过的方法:

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>

但是它仍然无法正常工作。

我认为正在发生的事情是绑定只是行不通。别人能看到什么问题吗?预先非常感谢。

0 个答案:

没有答案