如何绑定DataGridTextColumn可见性

时间:2011-11-03 17:21:34

标签: binding silverlight-4.0 datagrid

我正试图将此数据网格的可见性绑定为GreatTall1在其帖子中建议的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"));
        }
    }
}

}

我已将该类添加到我的解决方案中,并尝试将DataGridTextColumn可见性绑定到我的IdVisibility属性

<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"); }
    }

无论IdVisibility的值如何,都始终显示该列。

拜托,有人能指出我做错了什么吗?这是一款SL4应用

由于

1 个答案:

答案 0 :(得分:0)

我认为您应该使用可见性转换器也可以使用此转换器与Designer绑定

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