How to hide a control if the underlying DataContext is null?

回眸只為那壹抹淺笑 提交于 2019-11-30 08:05:01
Prince Ashitaka

Have a converter like follows,

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

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Now, bind the property with the Visibility property as well. Like,

<ListBox ItemsSource="{Binding Path=Squad}" 
         Visibility="{Binding Converter={StaticResource nullToVisibilityConverter}, 
                              Path=Squad}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

This approach is easier:

<CheckBox Visibility="{Binding Path=checkedField, TargetNullValue=Collapsed }">

When the bound property checkedField is null, the Visibility will be set to Collapsed.

Camilo Martinez

I also needed this for a WindowsPhone WinRT app. I ended up using @PrinceAshitaka's converter with a minor modification in the binding as suggested in this answer to a similar question

You should use FallbackValue=Collapsed to avoid showing the control precisely when the datacontext is null. Not sure why TargetNullValue=Collapsed didn't work for me.

Visibility="{Binding Converter={StaticResource NullToVisibilityConverter}, FallbackValue=Collapsed}"
Vladimir Dorokhov

In Silverlight you can use next approach - add trigger to control:

<i:Interaction.Triggers>
    <core:DataTrigger Binding="{Binding SomeProperty}" Comparison="Equal" Value="{x:Null}">
        <core:ChangePropertyAction PropertyName="Visibility" Value="Collapsed" />
    </core:DataTrigger>
</i:Interaction.Triggers>

You could use the DataContextChanged event, when the DataContext is null you could set the Visbility to Collapsed

read more about it here

Needed this, but I couldn't get it to work within a DataTemplate inside a DataGridTemplateColumn, so here is my example of how I got it to work.

 <DataGridTemplateColumn>
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
            <ComboBox ItemsSource="{Binding Path=DataContext.AvailableHierarchies, 
                                            RelativeSource={RelativeSource FindAncestor, 
                                            AncestorType={x:Type ItemsControl}} }"
                      DisplayMemberPath="Name"
                      SelectedItem="{Binding Path=DataContext.SelectedHierarchy, 
                                             RelativeSource={RelativeSource FindAncestor, 
                                             AncestorType={x:Type ItemsControl}},UpdateSourceTrigger=PropertyChanged }"
                                      >
            <ComboBox.Style>
                <Style TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
                    <Style.Triggers>
                        <Trigger Property="ComboBox.ItemsSource" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ComboBox.Style>
        </ComboBox>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!