如何在xaml中按条件隐藏groupbox

时间:2017-12-04 06:59:28

标签: wpf xaml

我有这段代码:

 <GroupBox Header="Varování" Grid.Column="0" VerticalAlignment="Top"  Grid.ColumnSpan="2" Height="Auto" Margin="0,75,0,-6" >



            <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Height="Auto">
                <checks:UCWeighingRecordCheck x:Name="weighingRecordCheck" DataContext="{Binding EntityNotifiedTruck.WeighingRecordCheck}"  VerticalAlignment="Top" />
            </StackPanel>
        </GroupBox>

我想要做的是:如果UCWeighingRecordChecks为空,我想隐藏此组框。有什么提示怎么做?

2 个答案:

答案 0 :(得分:1)

您还可以使用DataTrigger

<GroupBox Header="Varování" Grid.Column="0" VerticalAlignment="Top"  Grid.ColumnSpan="2" Height="Auto" Margin="0,75,0,-6" >
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Height="Auto">
       <checks:UCWeighingRecordCheck x:Name="weighingRecordCheck" DataContext="{Binding EntityNotifiedTruck.WeighingRecordCheck}"  VerticalAlignment="Top" />
   </StackPanel>
   <GroupBox.Style>
     <Style TargetType="GroupBox">
       <Style.Triggers>
          <DataTrigger Binding="{Binding UCWeighingRecordChecks }" Value="{x:Null}">
             <Setter Property="Visibility" Value="Collapsed"/>
          </DataTrigger>
        </Style.Triggers>
    </Style>
  </GroupBox.Style>
</GroupBox>

答案 1 :(得分:0)

您可以使用绑定和转换器:

资源中的

<NullToVisibilityConverter x:Key="NullToVis" />

然后在你的定义中:

<GroupBox Visibility="{Binding UCWeighingRecordChecks , Converter={StaticResource NullToVis}}" .../>

并使用此转换器:

[ValueConversion(typeof(object), typeof(Visibility))]
public sealed class NullToVisibilityConverter : IValueConverter
{

    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        if (value != null)
            return Visibility.Visible;
        else return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return null;
    }
}