1

I am trying to bind the visibility of datagrid as GreatTall1 suggested in his post from this thread

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace XYZ.Controls
{
public class ExtendedDataGridTextColumn : DataGridTextColumn
{
private readonly Notifier _e;
private Binding _visibilityBinding;
public Binding VisibilityBinding
{
    get { return _visibilityBinding; }
    set
    {
        _visibilityBinding = value;
        _e.SetBinding(Notifier.MyVisibilityProperty, _visibilityBinding);
    }
}
public ExtendedDataGridTextColumn()
{
    _e = new Notifier();
    _e.PropertyChanged += ToggleVisibility;
}

private void ToggleVisibility(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "Visibility")
        this.Visibility = _e.MyVisibility;
}

//Notifier class is just used to pass the property changed event back to the column   container Dependency Object, leaving it as a private inner class for now
private class Notifier : FrameworkElement, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Visibility MyVisibility
    {
        get { return (Visibility)GetValue(MyVisibilityProperty); }
        private set { SetValue(MyVisibilityProperty, value); }
    }

    public static readonly DependencyProperty MyVisibilityProperty = DependencyProperty.Register("MyVisibility", typeof(Visibility), typeof(Notifier), new PropertyMetadata(MyVisibilityChanged));

    private static void MyVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var n = d as Notifier;
        if (n != null)
        {
           // n.MyVisibility = (Visibility) e.NewValue;
            n.PropertyChanged(n, new PropertyChangedEventArgs("Visibility"));
        }
    }
}

}

I have added the class to my solution, and try to bind the DataGridTextColumn visibility to my IdVisibility property

<sdk:DataGrid x:Name="dgResult" Grid.Row="0" VerticalAlignment="Stretch" SelectionMode="Single" HorizontalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding IdSearchList}" IsReadOnly="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Margin="10" Visibility="{Binding IsResultVisible}">

<extDg:ExtendedDataGridTextColumn Binding="{Binding Id}" VisibilityBinding="{Binding IdVisibility}"  Header="Asset ID" Width="50"/>



    private Visibility idVisibility = Visibility.Collapsed;

    public Visibility IdVisibility
    {
        get { return idVisibility; }
        set { idVisibility = value; NotifyPropertyChanged("IdVisibility"); }
    }

The column is always being displayed regardless of the value of IdVisibility.

Please, could someone point out what i am doing wrong? This is a SL4 app

Thanks

Community
  • 1
  • 1
MCS
  • 162
  • 1
  • 13

1 Answers1

0

I Think You Should Use Visiblity Converter Also You Can Bind With Designer with This Converter

Public NotInheritable Class VisibilityConverter
        Implements IValueConverter

        Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert

            If Not targetType Is GetType(Visibility) Then

                Throw New ArgumentOutOfRangeException("targetType", "VisibilityConverter can only convert to Visibility")

            End If

            Dim _visibility As Visibility = Visibility.Visible

            If value Is Nothing Then

                _visibility = Visibility.Collapsed

            End If

            If TypeOf (value) Is Boolean Then

                _visibility = IIf(value, Visibility.Visible, Visibility.Collapsed)

            End If

            If TypeOf (value) Is String Then

                _visibility = IIf(String.IsNullOrEmpty(value), Visibility.Collapsed, Visibility.Visible)

            End If

            Return _visibility

        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack

            Throw New ArgumentOutOfRangeException("targetType", "VisibilityConverter can only convert to Boolean")

        End Function
    End Class
Jignesh.Raj
  • 5,776
  • 4
  • 27
  • 56