Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 标签不为空时隐藏按钮_C#_Wpf - Fatal编程技术网

C# 标签不为空时隐藏按钮

C# 标签不为空时隐藏按钮,c#,wpf,C#,Wpf,我正在使用WPF 我想在标签不为空时显示按钮。当标签有值时,按钮将隐藏 我怎样才能用WPF做到这一点?使用 代码: <Label Name="lblCustomerName"/> <Button Name="btnCustomer" Content="X" Visibility="Hidden" /> 试试您需要使用转换器并将其绑定到lblCustomer的内容 public class ContentNullToVisibilityConverter : IValue

我正在使用WPF

我想在标签不为空时显示按钮。当标签有值时,按钮将隐藏

我怎样才能用WPF做到这一点?使用

代码:

<Label Name="lblCustomerName"/>
<Button Name="btnCustomer" Content="X" Visibility="Hidden" />

试试

您需要使用转换器并将其绑定到lblCustomer的内容

public class ContentNullToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return Visibility.Hidden;
        }
        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
更多关于转换器的信息

然后在xaml中,您可以执行以下操作:

第一行需要在您的资源中定义,您需要使用您在其中创建上述类的名称空间对其进行限定。一旦定义了资源,就可以使用第二部分

<ContentNullToVisibilityConverter x:Key="visConverter"/>

<Label Name="lblCustomerName"/>
<Button Name="btnCustomer" Content="X" Visibility="{Binding ElementName=lblCustomer, Path=Content, Converter={StaticResource visConverter}}" />

<ContentNullToVisibilityConverter x:Key="visConverter"/>

<Label Name="lblCustomerName"/>
<Button Name="btnCustomer" Content="X" Visibility="{Binding ElementName=lblCustomer, Path=Content, Converter={StaticResource visConverter}}" />