locked
wpf datagridtextcolumn visibility converter RRS feed

  • Question

  • i have  a datagrid in my xaml.
    Ineed to hide a column based upon some conditions.I tried using a converter.But the converter code is not hit ever.Any help is appreciated
    Thursday, May 2, 2013 2:20 PM

Answers

  • Hi ams16,

    The reason why you can’t get the covert hit is : column in a datagrid is an abstract object which does not appear in the visual tree, thus you cannot use RelativeSource-binding, ElementName will not work.

    However, you can try to use convert via Source and x:Reference.

    Here is a simple demo you can refer to:

    <Window  x:Class="WpfApplication1.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication1"
           
           Title="MainWindow"  Height="550" Width="555"  >
        <Window.Resources>
            <local:BooleanToVisibilityConverter x:Key="test"/>
            <DataGridTextColumn  x:Key="NumColumn"  Header="test" Binding="{Binding Number}"  
                                 Visibility="{Binding Path=Text, Source={x:Reference Status},Converter={StaticResource test}}"/>
    
        </Window.Resources>
        <StackPanel>
            <TextBox Name="Status"></TextBox>  
                <DataGrid Name="dg" AutoGenerateColumns="False">
                    <DataGrid.Columns>
                    <StaticResource  ResourceKey="NumColumn"/>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>    
                        <DataGridTextColumn  Header="Number" Binding="{Binding Number}" />            
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel> 
    
    </Window>

    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
          
    
            public MainWindow()
            {
                InitializeComponent();
    
                Status.Text = "5.0";
                dg.ItemsSource = CreateTable().DefaultView;
              
            }
    
            public DataTable CreateTable()
            {
                DataTable dt = new DataTable();
    
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Number", typeof(double));
                dt.Columns.Add("Image",typeof(string));
    
                dt.Rows.Add("A", 12, @"D:\img\2.jpg");
                dt.Rows.Add("B", 120, "D:\\img\\3.jpg");
                dt.Rows.Add("C", 10, "D:\\img\\4.jpg");
                dt.Rows.Add("D", 20, "D:\\img\\5.jpg");
                dt.Rows.Add("E", 3, "D:\\img\\6.jpg");
    
                return dt;
            }
    
       }//MainWindow     
    
        public class BooleanToVisibilityConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string s = (string)value.ToString();
                if (s != "")
                {
                    double number = double.Parse(s);
                    if (number == 5.0)
                    {
                        return Visibility.Hidden;
                    }
                    else
                        return Visibility.Visible;
                }
                else
                    return Visibility.Visible;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return null;
            }
    
        }
    
    }    //WpfApplication1

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jonatan Dragon Friday, May 10, 2013 6:50 AM
    • Marked as answer by Lisa Zhu Monday, May 13, 2013 9:30 AM
    Friday, May 10, 2013 3:14 AM

All replies

  • We are not magicians, give us some code.

    However, check your output windows to see any binding errors.

    Also make sure, the property you bind to is changed and raises PropertyChanged event or is a DependencyProperty.

    Greetings

    Thursday, May 2, 2013 2:36 PM
  • basically I am implementing a IValueConverter and binding it to visibility of the column



    <DataGridTextColumn Header="Column1" Visibility="{Binding ElementName=Status,Converter={StaticResource BooleanToVisibilityConverter}}"/>
    Thursday, May 2, 2013 4:29 PM
  • So, as I said higher, if you do not get any binding errors in the Output window,your problem is probably caused by no value change, or by no notifying about the change. Check out this and this.
    Thursday, May 2, 2013 6:15 PM
  • Hi ams16,

    The reason why you can’t get the covert hit is : column in a datagrid is an abstract object which does not appear in the visual tree, thus you cannot use RelativeSource-binding, ElementName will not work.

    However, you can try to use convert via Source and x:Reference.

    Here is a simple demo you can refer to:

    <Window  x:Class="WpfApplication1.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication1"
           
           Title="MainWindow"  Height="550" Width="555"  >
        <Window.Resources>
            <local:BooleanToVisibilityConverter x:Key="test"/>
            <DataGridTextColumn  x:Key="NumColumn"  Header="test" Binding="{Binding Number}"  
                                 Visibility="{Binding Path=Text, Source={x:Reference Status},Converter={StaticResource test}}"/>
    
        </Window.Resources>
        <StackPanel>
            <TextBox Name="Status"></TextBox>  
                <DataGrid Name="dg" AutoGenerateColumns="False">
                    <DataGrid.Columns>
                    <StaticResource  ResourceKey="NumColumn"/>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>    
                        <DataGridTextColumn  Header="Number" Binding="{Binding Number}" />            
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel> 
    
    </Window>

    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
          
    
            public MainWindow()
            {
                InitializeComponent();
    
                Status.Text = "5.0";
                dg.ItemsSource = CreateTable().DefaultView;
              
            }
    
            public DataTable CreateTable()
            {
                DataTable dt = new DataTable();
    
                dt.Columns.Add("Name", typeof(string));
                dt.Columns.Add("Number", typeof(double));
                dt.Columns.Add("Image",typeof(string));
    
                dt.Rows.Add("A", 12, @"D:\img\2.jpg");
                dt.Rows.Add("B", 120, "D:\\img\\3.jpg");
                dt.Rows.Add("C", 10, "D:\\img\\4.jpg");
                dt.Rows.Add("D", 20, "D:\\img\\5.jpg");
                dt.Rows.Add("E", 3, "D:\\img\\6.jpg");
    
                return dt;
            }
    
       }//MainWindow     
    
        public class BooleanToVisibilityConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string s = (string)value.ToString();
                if (s != "")
                {
                    double number = double.Parse(s);
                    if (number == 5.0)
                    {
                        return Visibility.Hidden;
                    }
                    else
                        return Visibility.Visible;
                }
                else
                    return Visibility.Visible;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return null;
            }
    
        }
    
    }    //WpfApplication1

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jonatan Dragon Friday, May 10, 2013 6:50 AM
    • Marked as answer by Lisa Zhu Monday, May 13, 2013 9:30 AM
    Friday, May 10, 2013 3:14 AM