WPF DataGridTemplateColumn,CellTemplate?如何“隐藏”按钮

cwtwac6a  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(206)

我有一个站立的状态。Text =“ADMIN”;(在cs)如果这是管理员,那么我如何隐藏前3个按钮,请给予意见

<DataGridTemplateColumn Header="Operations" IsReadOnly="True" Width="*">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{StaticResource gridInfButton}" x:Name="infButton">
                                            <Icon:PackIconMaterial Kind="Information" Style="{StaticResource gridButtonIcon}"/>
                                        </Button>
                                        <Button Style="{StaticResource gridPlayButton}" x:Name="playButton">
                                            <Icon:PackIconMaterial Kind="PlayOutline" Style="{StaticResource gridButtonIcon}"/>
                                        </Button>
                                        <Button Style="{StaticResource gridEditButton}" Margin="5 0 0 0" x:Name="editButton">
                                            <Icon:PackIconMaterial Kind="PencilOutline" Style="{StaticResource gridButtonIcon}"/>
                                        </Button>
                                        <Button Style="{StaticResource gridRemoveButton}" Margin="5 0 0 0" x:Name="removeButton" IsEnabled="False">
                                            <Icon:PackIconMaterial Kind="DeleteOutline" Style="{StaticResource gridButtonIcon}"/>
                                        </Button>
                                        <Button Style="{StaticResource gridEyeButton}" Margin="5 0 0 0" x:Name="eyeButton">
                                            <Icon:PackIconMaterial Kind="EyeOutline" Style="{StaticResource gridButtonIcon}"/>
                                        </Button>
                                    </StackPanel>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

我试着用各种方法做这件事,但对我都没有效果
在此输入

wb1gzix0

wb1gzix01#

您可以在xaml的按钮声明中设置Visibility = "{Binding IsAdmin}",并创建一个属性public Visibility IsAdmin {get;set;},该属性在更改时发出通知(如果您使用MVVM,则应该很容易)。如果你不使用MVVM,你可以创建一个类:

public class NotifyingProperty<T> : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private T _property;

    public T Property
    {
        get { return _property; }
        set 
        { 
            _property = value;
            NotifyPropertyChanged();
        }
    }

    public NotifyingProperty()
    {
        this._property = default;
    }

    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

然后在窗口后端创建一个属性public NotifyingProperty<Visibility> IsAdmin {get;set;},并在文本更改时设置它。
注意:在这种情况下,必须将"{Binding IsAdmin}"更改为“{Binding IsAdmin.Property}"
另外,不要忘记在这两种情况下调用DataContext = this;

相关问题