1

I have value converter that converts the visibilty aspect of a control when its text starts with http or not. Below is the code.

public class HTTPVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool boolVisbility = (value != null) && value.ToString().StartsWith("http");

        boolVisbility = (parameter != null) ? !boolVisbility : boolVisbility;

        return boolVisbility ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I have put this code in the ToolViewe.xaml.cs . I am trying to use the above converter in my datagrid as follows:

<slData:DataGrid x:Name="CustomerDetailsDataGrid"   
 ItemsSource=" {Binding     SingleReturnedItemAttributes}"  
 AutoGenerateColumns="False"HeadersVisibility="None"   
 ScrollViewer.HorizontalScrollBarVisibility="Visible"  
 ScrollViewer.VerticalScrollBarVisibility="Visible" MaxHeight="350"   
 MaxWidth="350">

<slData:DataGrid.Columns>
<slData:DataGridTextColumn    Binding="  {Binding Path=Key}" 
 FontWeight="Bold"/>
 <slData:DataGridTextColumn Binding="{Binding Path= Value}" Visibility="
 {Binding Path=IsControlVisible,Converter={StaticResource theHttpVisbilityConverter}}" 
 />
<slData:DataGridTextColumn Binding="{Binding Path= Value}" Visibility="
 {Binding Path=IsControlVisible ,Converter={StaticResource theHttpVisbilityConverter}, 
  ConverterParameter=reverse}" 
 />
 </slData:DataGrid.Columns>

What should I be using for the Binding Path of the Visibility? I have tried using the following property by declaraing the code in ToolViewModel.cs. But does not work. Please guide this newbie.

bool isControlVisible = false;
    public bool IsControlVisible
    {
        get { return isControlVisible; }
        set
        {
            isControlVisible = value;
            this.RaisePropertyChanged(() => this.IsControlVisible);
        }
    }

This is the error: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Windows.Visibility'.

ayaantara
  • 41
  • 6
  • Not quite clear what you're trying to do, if you want to hide the entire column then you'll need to use a [binding proxy](http://stackoverflow.com/questions/12144561/bind-datagridtextcolumn-visibility-property-in-wpf). If you want to hide individual cells then you'll need to [set the element style](http://stackoverflow.com/questions/20352231/create-style-for-textblock-in-datagridtextcolumn) instead. Both cases are duplicates. – Mark Feldman Sep 01 '15 at 23:10
  • Hi Mark, what you said is true. There would be two columns and one cell of a row would have a url . I just need to have the url as link or if text does not contain : http then it would show as plain text. It looks like this – ayaantara Sep 02 '15 at 04:51

1 Answers1

0

Finally I was able to crack down what was the reason behind my error:

I had create the dependency for the visibility of datagridtextcolumn and the datagridtemplatecolumn in the xaml.cs.

public class VisibilityData : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool _visible = true;

    public bool Visible
    {
        get { return this._visible; }
        set
        {
            if (this._visible != value)
            {
                this._visible = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Visible"));
                }
            }
        }
    }
}


public class ExtendedDataGridTextColumn : DataGridTextColumn
{
    public Visibility MyVisibility
    {
        get
        {
            return (Visibility)GetValue(MyVisibilityProperty);
        }
        set
        {
            SetValue(MyVisibilityProperty, value);
        }
    }

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

    private static void MyVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var n = d as ExtendedDataGridTextColumn;
        if (n != null && e.NewValue is Visibility)
        {
            n.Visibility = (Visibility)e.NewValue;
        }
    }

    public ExtendedDataGridTextColumn()
    {
    }

}

public class CustomDataGridTemplateColumn : DataGridTemplateColumn
{
    public static readonly DependencyProperty VisibilityBindingProperty = DependencyProperty.Register(
      "VisibilityBinding", typeof(Visibility), typeof(CustomDataGridTemplateColumn), new PropertyMetadata(Visibility.Collapsed, new PropertyChangedCallback(OnVisibilityChanged)));

    public Visibility VisibilityBinding
    {
        get { return (Visibility)this.GetValue(VisibilityBindingProperty); }
        set { this.SetValue(VisibilityBindingProperty, value); }
    }

    private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((CustomDataGridTemplateColumn)d).Visibility = (Visibility)e.NewValue;
    }

}

And now this is how my datagrid looks like:

<slData:DataGrid x:Name="Customer" ItemsSource="{Binding ReturnedItemAttributes}" AutoGenerateColumns="False"  HeadersVisibility="None" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" MaxHeight="350" MaxWidth="350">

                        <slData:DataGrid.Resources>
                            <ResourceDictionary>
                                <local:HTTPVisibilityConverter x:Key="httpVisibility"/>
                                <local:VisibilityData x:Key="visibilityValue"/>
                            </ResourceDictionary>
                        </slData:DataGrid.Resources>
                        <slData:DataGrid.Columns>
                            <local:ExtendedDataGridTextColumn Binding="{Binding Path=Key}" FontWeight="Bold"/>                                
                            <local:ExtendedDataGridTextColumn Binding="{Binding Path=Value}" MyVisibility="{Binding Visible, Source={StaticResource visibilityValue}, Converter={StaticResource httpVisibility}, ConverterParameter=Reverse}"/>

                            <local:CustomDataGridTemplateColumn Width="40"  VisibilityBinding="{Binding Visible, Source={StaticResource visibilityValue}, Converter={StaticResource httpVisibility}}">
                                <local:CustomDataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                        <HyperlinkButton Content="Pdf Link" Height="23" Name="PdfUrl" Width="76" NavigateUri="{Binding Path=Value, Mode=TwoWay}"    TargetName="_blank" Margin="5,10,10,7" Foreground="Blue" />
                                    </DataTemplate>
                                </local:CustomDataGridTemplateColumn.CellTemplate>
                            </local:CustomDataGridTemplateColumn>

                        </slData:DataGrid.Columns>
                    </slData:DataGrid>

This is how i resolved my previous errors.

So now i can see the pdf hyperlink in a separate column but my converter for the datatagridtemplate column does not seem to work. It works for the datagridtextcolumn though. Can someone please specify how to hide the datagridtemplatecoulumn using my converter. and also now my grid show as this

 Column1  column2  Column3
    name    John     pdf
    CustomerID   1234   pdf
    Customer_url  hhtp://www.test.com  pdf

How can i remove the pdf link from the other rows and keep it only for the Customer url row. Thank You

ayaantara
  • 41
  • 6