Reputation: 229
I want hide a label visibility by another content, suppose I have a GroupBox which contains a DataGrid:
<GroupBox>
<DataGrid x:Name="Dt">
<DataGrid.Columns>
<DataGridTextColumn Header="home" />
</DataGrid.Columns>
</DataGrid>
</GroupBox>
I want insert a Label
which appear only when I hide the DataGrid
, so I tried a simple StackPanel
:
<GroupBox>
<StackPanel Orientation="Vertical">
<DataGrid x:Name="Dt">
<DataGrid.Columns>
<DataGridTextColumn Header="home" />
</DataGrid.Columns>
</DataGrid>
<Label Content="Foo" />
</StackPanel>
</GroupBox>
so I tried to set the visibility of DataGrid
behind code, like:
Dt.Visibility = Visibility.Hidden;
this will hide the DataGrid
but the main problem is the label, in fact the label is even visible, also if the Dt
is visibile.
How can I display the label only when the DataGrid is hidden?
Upvotes: 0
Views: 505
Reputation: 15
You can use the Visibility property of the Datagrid element and assign to the label when grid is visible label also will be visible.
<Label Content="Foo" Visibility="{Binding ElementName=Dt, Path=Visibility}" />
Upvotes: 0
Reputation: 1864
You could bind the visibility of the label to datagrid visibility and use a converter that switch the value:
<GroupBox>
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<local:VisibilityInverter x:key="VisInverter"/>
</StackPanel.Resources>
<DataGrid x:Name="Dt">
<DataGrid.Columns>
<DataGridTextColumn Header="home" />
</DataGrid.Columns>
</DataGrid>
<Label Content="Foo" Visibility="{Binding Path=Visibility, ElementName=Dt, Converter={StaticResource VisInverter}}" />
</StackPanel>
</GroupBox>
Here the converter:
public class VisibilityInverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return Visibility.Hidden;
if (value == Visibility.Visible)
return Visibility.Hidden;
return Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, ultureInfo culture)
{
throw new NotImplementedException();
}
}
Upvotes: 1
Reputation: 169200
You could apply a Style
to the Label
with a DataTrigger
that binds to the Visibility
property of the DataGrid
:
<GroupBox>
<StackPanel Orientation="Vertical">
<DataGrid x:Name="Dt" Visibility="Collapsed">
<DataGrid.Columns>
<DataGridTextColumn Header="home" />
</DataGrid.Columns>
</DataGrid>
<Label Content="Foo">
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<DataTrigger Binding="{Binding Visibility, ElementName=Dt}" Value="Visible">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</StackPanel>
</GroupBox>
Using this XAML only approach you could bind to any property of the DataGrid
. You could for example bind to the HasItems
property and only display the Label
when the DataGrid
is empty. No converters needed.
Upvotes: 1
Reputation: 71
You can default the Label's visibility to Hidden and in the code behind, when the DataGrid is hidden in whatever method you adjust that, you set the visibility of the label to Visible
XAML:
<GroupBox>
<StackPanel Orientation="Vertical">
<DataGrid x:Name="Dt">
<DataGrid.Columns>
<DataGridTextColumn Header="home" />
</DataGrid.Columns>
</DataGrid>
<Label Content="Foo" Visibility="Hidden" x:Name="fooLabel" />
</StackPanel>
</GroupBox>
Code Behind:
private void yourMethod(object sender, RoutedEventArgs e)
{
Dt.Visibility = Visibility.Hidden;
fooLabel.Visibility = Visibility.Visible;
}
Upvotes: 1
Reputation: 35681
use Grid
panel and place Label before DataGrid. They are positioned in the same grid cell and this will ensure that Label is covered by DataGrid when it is visible.
<GroupBox>
<Grid>
<Label Content="Foo" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<DataGrid x:Name="Dt">
<DataGrid.Columns>
<DataGridTextColumn Header="home" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</GroupBox>
Upvotes: 1