Skip to content

Infragistics Community Forum / Cross Platform / Ultimate UI for Xamarin / XamDataGrid, Collapsing a column based on a visibility value

XamDataGrid, Collapsing a column based on a visibility value

New Discussion
jai sundar
jai sundar asked on Jul 28, 2014 6:32 AM

Hi,

I have already seen a few examples about this issue. But i am not able to correlate to my problem. Please help me in doing it.

My problem is that i have a datagrid with two fields and i want to collapse one field based on a visibility value change in the code.

The XAML code part where i define the fields is as follows :

<igDP:XamDataGrid.FieldLayouts>

<igDP:FieldLayout>

<igDP:FieldLayout.Fields>

<igDP:Field Visibility=”{Binding Source={StaticResource ViewModel}, Path=bRValidation_Display}” Name=”BO” Label=”BLine Operation” />

<igDP:Field Name=”ROperation” Label=”ROperation”/>

igDP:FieldLayout.Fields>

igDP:FieldLayout>

igDP:XamDataGrid.FieldLayouts>

and the resources is defined in the start as

<Grid.Resources>

<rvm:OViewerViewModel x:Key=”ViewModel”/>

</Grid.Resources>

and rvm namespace is defined in the starting !!

 

 

the C# code where i define the visibility variable is

public Visibility bRValidation_Display

{

get { return

bVisibility; }

set

{

bVisibility =value;

NotifyPropertyChanged(“bRValidation_Display”);

}

This visibility value is set in the later part of the code . I tried to check the values using breakpoints and found that the visibility value of the BrValidation_Display when checked in the XAML code in the fields line during execution can be seen as collapsed . but the output window still shows the field in the grid without collapsing it . Is the value not binded properly or the problem is related to the visual tree. Where am i going wrong. Please provide me a way to correct this as well.

Sign In to post a reply

Replies

  • 0
    [Infragistics] Stefana Andreeva
    [Infragistics] Stefana Andreeva answered on Mar 25, 2013 4:50 PM

    Hi Jai,

     

    I believe this case refers to the WPF platform, so I have prepared a very simple application where I am trying to reproduce the described behavior.

    You can see the attached solution and contact me for any further help if necessary.

     

    Thanks,

    Stefana

     

     

    WpfDataGrid

    • 0
      jai sundar
      jai sundar answered on Mar 31, 2013 11:04 AM

      Hi Stefana,

       

      Thanks for your example !

      I was going through my code and found out that it was different from the example u had mentioned.

      In my case only after the views are created( i.e, after the InitializeComponent() ) the viewmodel is assigned as the datacontext of the view !!

      Hence only after the window is loaded the datacontext is available !! The problem is that i can't change the code which is already present before!

       

      Is there a way to use the datacontext available, after the window is loaded, to change the visibility of the field in the datagrid ??

       

      Regards,

      Jai

      • 0
        [Infragistics] Stefana Andreeva
        [Infragistics] Stefana Andreeva answered on Apr 1, 2013 7:51 AM

        Hi Jai,

        I have slightly modified the sample – the IsChecked property is now bound to the data context in XAML.

        Have a look at the newly attached solution. If this is not your scenario, could you please provide your sample, so that I can take a deeper look at the problem.

         

        Thanks,

        Stefana

         

        WpfDataGrid (1)

      • 0
        Stefan
        Stefan answered on Apr 8, 2013 2:31 PM

        Hello Jai,

         

        I am just checking the progress of this issue and was wondering if you managed to achieve your goal or if you need any further assistance on the matter.

         

        If the above suggestion helped you solve your issue please verify the thread as answered so other users may take better advantage of it.

      • 0
        Jon Heaton
        Jon Heaton answered on Jul 24, 2014 10:39 PM

        I am having a similar issue except with a combobox instead of a checkbox.

        Any help greatly appreciated.

        Here is my code:

        MainWindow.xaml

        <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
        xmlns:igDP="http://infragistics.com/DataPresenter&quot;
        xmlns:wpfApplication1="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
        <wpfApplication1:Value1VisibilityConverter x:Key="Value1VisibilityConverter" />
        <wpfApplication1:Value2VisibilityConverter x:Key="Value2VisibilityConverter" />
        </Window.Resources>
        <Grid>
        <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0" Margin="10 0 0 10" >
        <TextBlock VerticalAlignment="Center">Which Value?</TextBlock>
        <ComboBox Width="80" Margin="10 0 0 0" SelectedIndex="{Binding SelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
        <ComboBoxItem>Value 1</ComboBoxItem>
        <ComboBoxItem>Value 2</ComboBoxItem>
        </ComboBox>
        </StackPanel>
        <igDP:XamDataGrid VerticalAlignment="Top"
        Grid.Row="1"
        AutoFit="True"
        GroupByAreaLocation="None"
        Theme="Office2013"
        DataSource="{Binding GridValues}">
        <igDP:XamDataGrid.FieldLayoutSettings>
        <igDP:FieldLayoutSettings AutoGenerateFields="False"/>
        </igDP:XamDataGrid.FieldLayoutSettings>
        <igDP:XamDataGrid.FieldLayouts>
        <igDP:FieldLayout>
        <igDP:UnboundField Label="Product" Binding="{Binding ProductName}"/>
        <igDP:UnboundField Label="Value1" Width="100" Binding="{Binding Value1, Mode=TwoWay}" Visibility="{Binding SelectedIndex, Converter={StaticResource Value1VisibilityConverter}, UpdateSourceTrigger=PropertyChanged}"/>
        <igDP:UnboundField Label="Value2" Width="100" Binding="{Binding Value2, Mode=TwoWay}" Visibility="{Binding SelectedIndex, Converter={StaticResource Value2VisibilityConverter}, UpdateSourceTrigger=PropertyChanged}"/>
        </igDP:FieldLayout>
        </igDP:XamDataGrid.FieldLayouts>
        </igDP:XamDataGrid>
        </Grid>
        </Window>

        MainWindow.xaml.cs

        using System;
        using System.Collections.ObjectModel;
        using System.ComponentModel;
        using System.Globalization;
        using System.Windows;
        using System.Windows.Data;

        namespace WpfApplication1
        {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
        public MainWindow()
        {
        InitializeComponent();
        MyVM vm = new MyVM();
        this.DataContext = vm;

        vm.GridValues = new ObservableCollection<GridRow>();
        vm.GridValues.Add(new GridRow() { ProductName = "Product 1", Value1 = 1, Value2 = 0 });
        vm.GridValues.Add(new GridRow() { ProductName = "Product 2", Value1 = 1, Value2 = 0 });
        vm.GridValues.Add(new GridRow() { ProductName = "Product 3", Value1 = 1, Value2 = 0 });
        vm.GridValues.Add(new GridRow() { ProductName = "Product 4", Value1 = 1, Value2 = 0 });
        vm.GridValues.Add(new GridRow() { ProductName = "Product 5", Value1 = 1, Value2 = 0 });
        vm.RaisePropertyChanged("GridValues");
        }
        }

        public class ViewModelBase : INotifyPropertyChanged
        {
        public void RaisePropertyChanged(String property)
        {
        if (this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

        public event PropertyChangedEventHandler PropertyChanged = null;
        }

        public class MyVM : ViewModelBase
        {
        private int _selectedIndex;

        public int SelectedIndex
        {
        get { return _selectedIndex; }
        set
        {
        _selectedIndex = value;
        this.RaisePropertyChanged("SelectedIndex");
        }
        }

        public ObservableCollection<GridRow> GridValues { get; set; }
        }

        public class GridRow : ViewModelBase
        {
        public String ProductName { get; set; }
        public Double Value1 { get; set; }
        public Double Value2 { get; set; }
        }

        internal class Value1VisibilityConverter : IValueConverter
        {
        #region IValueConverter Members
        public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
        return ((int)value) == 0 ? "Visible" : "Collapsed";
        }

        public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
        return value;
        }
        #endregion
        }

        internal class Value2VisibilityConverter : IValueConverter
        {
        #region IValueConverter Members
        public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
        return ((int)value) == 1 ? "Visible" : "Collapsed";
        }

        public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
        return value;
        }
        #endregion
        }
        }

      • 0
        Stefan
        Stefan answered on Jul 25, 2014 9:17 AM

        Hello Jon,

         

        Thank you for your post. I have been looking into it and I created a sample project for you using your code with a modification in the Bindings. Basically I create an instance of the ViewModel in the Windows’ resources and set it as DataContect to the Grid. I also use it as Source of the Field’s Bindings, because the Fields are not visual elements and they are not part of the VisualTree and they doesn’t inherit the DataContext. Please let me know if this helps you or you have further questions on this matter.

         

        Looking forward for your reply.

         

        XamDataGridFieldVisibility

      • 0
        Jon Heaton
        Jon Heaton answered on Jul 25, 2014 3:33 PM

        Is there a way to do this using RelativeSource?  I was hoping not to have to change the way I bind the view to the viewmodel.

      • 0
        Stefan
        Stefan answered on Jul 28, 2014 6:32 AM

        Hello Jon,

        As I said the Field element is not a visual element, so it doesn't belong to the VisualTree and it doesn't inherit the DataContext. This is why the Source Property of the Binding should be set.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
jai sundar
Favorites
0
Replies
8
Created On
Jul 28, 2014
Last Post
12 years, 1 month ago

Suggested Discussions

Tags

Created by

Created on

Jul 28, 2014 6:32 AM

Last activity on

Feb 23, 2026 3:53 PM