Silverlight5 - 从DataGrid绑定可见性

时间:2012-10-30 18:13:12

标签: silverlight

Silverlight5

当DataGrid中没有数据时,我试图让TextBlock可见。

DataGrid中要绑定的内容?

当我如下所示绑定到Visibility时,即使网格中没有数据,它也总是返回true。

    <TextBlock Grid.Row="4" HorizontalAlignment="Center" Text="No User Accounts found." 
Visibility="{Binding ElementName=AccountGridT32, Path=Visibility, 
Converter={StaticResource NegateVisibilityConverter}}" />

    <Grid Grid.Row="5">
    <sdk:DataGrid x:Name="AccountGridT32" 
ItemsSource="{Binding Data, ElementName=AccountsDataSourceT32}" Grid.Column="0" Grid.Row="1" 
    ScrollViewer.HorizontalScrollBarVisibility="Auto" />
    </Grid>

1 个答案:

答案 0 :(得分:0)

我所做的是将texblock的可见性绑定到列表框/网格的项目数...

<TextBlock Grid.Row="4" HorizontalAlignment="Center" Text="No User Accounts found." 
Visibility="{Binding ItemsSource.Count, ElementName=AccountGridT32, Converter={StaticResource listCountToVis}, ConverterParameter=true}" />

我的转换器:

public class ListCountToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || !(value is int))
                return Visibility.Collapsed;

            bool booInvert = false;
            if (parameter is string) bool.TryParse((string)parameter, out booInvert);

            if (booInvert) return ((int)value) <= 0 ? Visibility.Visible : Visibility.Collapsed;
            else return ((int)value) > 0 ? Visibility.Visible : Visibility.Collapsed;


        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

        #endregion
    }