I am trying to hide the column from the data grid by changing the visibility value of the field but, the column is still there as a placeholder, only the data on it goes invisible.
What am I missing ?
<infragistic:XamDataGrid Grid.Row="1" DataSource="{Binding LayerList }" PreviewKeyDown="DataGrid_PreviewKeyDown" Name="InputLayersDataGrid" GroupByAreaLocation="None"> <infragistic:XamDataGrid.FieldLayoutSettings> <infragistic:FieldLayoutSettings AutoGenerateFields="False" AutoFitMode="Always" AddNewRecordLocation="OnTopFixed" AllowAddNew="False" SupportDataErrorInfo="RecordsAndCells" DataErrorDisplayMode="Highlight" AllowDelete="True" SelectionTypeRecord="Single"/> </infragistic:XamDataGrid.FieldLayoutSettings> <infragistic:XamDataGrid.Resources> <Style x:Key="{x:Type infragistic:DataRecordCellArea}" TargetType="{x:Type infragistic:DataRecordCellArea}"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Record.DataItem.IsOk, UpdateSourceTrigger=PropertyChanged}" Value="false"> <Setter Property="BorderBrush" Value="#FFFFDC00"/> </DataTrigger> </Style.Triggers> </Style> <Style x:Key="style" TargetType="{x:Type infragistic:LabelPresenter}"> <Setter Property="Visibility" Value="{Binding DataContext.Is3D, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Converter ={StaticResource InverseBooltoVis}}"/> </Style> </infragistic:XamDataGrid.Resources> <infragistic:XamDataGrid.FieldLayouts> <infragistic:FieldLayout> <infragistic:FieldLayout.Fields> <infragistic:Field Name="TopHorizon" Label="Top horizon"></infragistic:Field> <infragistic:Field Name="BottomHorizon" Label="Bottom horizon"></infragistic:Field> <infragistic:Field Name="DepositionalMode" Label="Depositional mode"></infragistic:Field> <infragistic:TemplateField Name="TopHorizonMarker" Label="Top horizon marker"> <infragistic:TemplateField.Settings> <infragistic:FieldSettings LabelPresenterStyle="{StaticResource style}"/> </infragistic:TemplateField.Settings> <infragistic:TemplateField.EditorStyle> <Style TargetType="{x:Type igEdit:XamNumericEditor}"> <Setter Property="Visibility" Value="{Binding DataContext.Is3D, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Converter ={StaticResource InverseBooltoVis}}"/> </Style> </infragistic:TemplateField.EditorStyle> <infragistic:TemplateField.EditTemplate> <DataTemplate> <controls:SearchableDropBox DomainObject="{igEdit:TemplateEditorValueBinding UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" InputValidationManifest="{Binding Path=DataContext.SurfaceDataValidaton, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Mode=OneWay}" ToolTipHeader="Target guide model data " ToolTipText="Select a supported seismic type" ToolTipService.IsEnabled="True" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" Validation.ErrorTemplate="{StaticResource CommonErrorTemplate}" Name="GuideModelData"/> </DataTemplate> </infragistic:TemplateField.EditTemplate> <infragistic:TemplateField.DisplayTemplate> <DataTemplate> <controls:PresentationBox Name="GuideModelData" DomainObject="{igEdit:TemplateEditorValueBinding UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" Validation.ErrorTemplate="{StaticResource CommonErrorTemplate}" ></controls:PresentationBox> </DataTemplate> </infragistic:TemplateField.DisplayTemplate> </infragistic:TemplateField> </infragistic:FieldLayout.Fields> </infragistic:FieldLayout> </infragistic:XamDataGrid.FieldLayouts> </infragistic:XamDataGrid>
Hello,
Thank you for the code-snippet provided.
What I see from the code is that you're using ValueConverter in order to set value to the Visibility property. A possible reason why only the data goes invisible (and not the field) is if in the converter you return "Hidden" as a value for the Visibility property. In order to make the entire column invisible you could change it to "Collapsed" in case it is not.
Please test this suggestion on your side and let me know if I may be of any further assistance.
public class BoolToVisibilityConverter : BaseConverter { public override object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value ? Visibility.Visible : Visibility.Collapsed; } public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if ((Visibility)value == Visibility.Collapsed || (Visibility)value == Visibility.Hidden) return false; else { return true; } } }
public class InverseBooleanToVisibilityConverter : BaseConverter { public override object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value ? Visibility.Collapsed : Visibility.Visible; } public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if ((Visibility)value == Visibility.Collapsed || (Visibility)value == Visibility.Hidden) return true; else { return false; } } }
Both my converters return "Collapsed". I am using infragistic 18.1*
<Grid.Resources> <BooleanToVisibilityConverter x:Key="boolToVis"/> </Grid.Resources> <igDP:XamDataGrid Name="grid" DataSource="{Binding}" Grid.Row="0"> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AutoGenerateFields="False"/> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <igDP:Field Name="Name"/> <igDP:Field Name="Age" Visibility="{Binding Path=IsChecked, Source={x:Reference Name=visibility}, Converter={StaticResource boolToVis}}"/> <igDP:Field Name="Height"/> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts> </igDP:XamDataGrid> <CheckBox Name="visibility" Content="Visible/Invisible" Grid.Row="1" IsChecked="True"/>
<infragistic:XamDataGrid Grid.Row="1" DataSource="{Binding LayerList }" PreviewKeyDown="LayerGrid_PreviewKeyDown" Name="InputLayersDataGrid" GroupByAreaLocation="None" ActiveRecord="{Binding Path=SelectedLayer,Mode=TwoWay}"> <infragistic:XamDataGrid.FieldLayoutSettings> <infragistic:FieldLayoutSettings AutoGenerateFields="False" AutoFitMode="Always" AddNewRecordLocation="OnTopFixed" AllowAddNew="False" SupportDataErrorInfo="RecordsAndCells" DataErrorDisplayMode="Highlight" AllowDelete="True" SelectionTypeRecord="Single"/> </infragistic:XamDataGrid.FieldLayoutSettings> <infragistic:XamDataGrid.FieldLayouts> <infragistic:FieldLayout> <infragistic:FieldLayout.Fields> <infragistic:Field Name="TopHorizon" Label="Top horizon"></infragistic:Field> <infragistic:Field Name="BottomHorizon" Label="Bottom horizon"></infragistic:Field> <infragistic:ComboBoxField Name="DepositionalMode" Label="Depositional mode"></infragistic:ComboBoxField> <infragistic:TemplateField Name="TopHorizonMarker" Label="Top horizon marker" Visibility="{Binding DataContext.Is3D, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Converter ={StaticResource InverseBooltoVis}}"> <infragistic:TemplateField.EditTemplate> <DataTemplate> <controls:SearchableDropBox DomainObject="{igEdit:TemplateEditorValueBinding UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" InputValidationManifest="{Binding Path=DataContext.SurfaceDataValidaton, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Mode=OneWay}" ToolTipHeader="Target guide model data " ToolTipText="Select a supported seismic type" ToolTipService.IsEnabled="True" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" Validation.ErrorTemplate="{StaticResource CommonErrorTemplate}" Name="GuideModelData"/> </DataTemplate> </infragistic:TemplateField.EditTemplate> <infragistic:TemplateField.DisplayTemplate> <DataTemplate> <controls:PresentationBox Name="GuideModelData" DomainObject="{igEdit:TemplateEditorValueBinding UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" Validation.ErrorTemplate="{StaticResource CommonErrorTemplate}" ></controls:PresentationBox> </DataTemplate> </infragistic:TemplateField.DisplayTemplate> </infragistic:TemplateField> </infragistic:FieldLayout.Fields> </infragistic:FieldLayout> </infragistic:XamDataGrid.FieldLayouts> </infragistic:XamDataGrid>
I directly bind the field, but it is not working. I can't change the visibility, It stays as visible as default, even though I used the same exact code for binding.
I tried to reproduce behavior similar to what I saw in the provided code-snippet and modified the previously attached sample by creating ViewModel in which I had boolean property "Flag". Depending from its value, I set a value to visibility property of the field. If it is true the field would be visible, else it would be collapsed. When I bound the visibility property the way it is in the provided code-snippet, nothing happened as you described. However, as Output after the execution could be seen "Data:Error 2 Cannot find governing FrameworkElement or FrameworkContentElement ". The reason nothing happens is that the binding did not work due to the fact that Field is not part of the visual tree. It is not FrameworkElement or FrameworkContentElement.
In order to bind the field's visibility property the following should be provided: Source, Path and if there is need (like in this case) - Converter. The Source should be the ViewModel where is the location of our boolean variable. Because of that the ViewModel should be included in the grid's resources.
For the purposes of the example, I left the CheckBox at the bottom, in order to be able to see the differences in the binding for both element.
<Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.Resources> <BooleanToVisibilityConverter x:Key="boolToVis"/> <local:ViewModel x:Key="vm"/> </Grid.Resources> <igDP:XamDataGrid Name="grid" DataSource="{Binding Path=People}" Grid.Row="0"> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AutoGenerateFields="False"/> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <igDP:Field Name="Name"/> <igDP:Field Name="Age" Visibility="{Binding Source={StaticResource vm}, Path=Flag, Converter={StaticResource boolToVis}}"/> <igDP:Field Name="Height"/> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts> </igDP:XamDataGrid> <CheckBox Name="visibility" Content="Visible/Invisible" Grid.Row="1" IsChecked="{Binding DataContext.Flag, ElementName=uc, Mode=OneWay}"/> </Grid>
I have attached the modified sample below. Please test it on your side and let me know how it behaves. If this is not an accurate demonstration of what you are trying to achieve please feel free to modify it and send it back to me along with steps to reproduce. Alternatively, if the behavior cannot be replicated please feel free to provide your own sample. Remove any external dependencies and code that is not directly related to the issue, zip your application and attach in in this case.
Having a working sample on my side, which I can debug, is going to be very helpful in finding the root cause of this behavior.
Thank you for your cooperation.
Regards,Teodosia HristodorovaAssociate Software Developer
2577.XamDataGrid_Field_Visibility.zip
I don't know why but I can't upload the project, let me illustrate my problem with static resource,
My visibility property is something like this
public bool IsVisible { get { foreach (var person in People) { if (person.Age < 18) { return true; } } return false; } }
The visibility of the column is not bound to some property that I change, but the condition of some properties.
So in your example I bind it to the people list, if there is someone with the age lower than 18, the age column should visible if not invisible.
But since adding static resource create a new instance of the view model, this solution is not works as expected.
Is there any other way?
<igWPF:XamDataGrid.FieldLayoutSettings> <igWPF:FieldLayoutSettings AutoGenerateFields="False"/> </igWPF:XamDataGrid.FieldLayoutSettings> <igWPF:XamDataGrid.FieldLayouts> <igWPF:FieldLayout> <igWPF:FieldLayout.Fields> <igWPF:TextField Name="Name" /> <igWPF:Field Name="Age" Visibility="{igWPF:FieldBinding IsVisible, Converter={StaticResource boolToVis}}"/> </igWPF:FieldLayout.Fields> </igWPF:FieldLayout> </igWPF:XamDataGrid.FieldLayouts>
thank you, it works as expected
I am glad that you find my suggestion helpful.
Thank you for using Infragistics components.
Sincerely,Teodosia HristodorovaAssociate Software Developer