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

帅比萌擦擦* 提交于 2019-11-29 10:59:05

问题


I have an object in my view model that has a bunch of properties, some of them will occasionally be null. I don't want to just show some controls if these particular controls are null. How would I go about hiding the control if the bind is null? I was thinking of some sort of converter, but don't know how I'd go about doing it exactly. Any ideas?

edit: sorry, I should mention that this will also be in Silverlight, so I'm not sure if Style triggers would work...?


回答1:


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>



回答2:


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.




回答3:


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



回答4:


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>



回答5:


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

read more about it here




回答6:


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>


来源:https://stackoverflow.com/questions/5323817/how-to-hide-a-control-if-the-underlying-datacontext-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!