将字符串列表与布尔列表进行比较

时间:2013-06-13 12:01:56

标签: c# .net wpf mvvm

目前我这样做是为了设置这么多布尔属性的可见性。我想要一个泛型方法来获取布尔属性列表和字符串列表,然后在列表中存在特定字符串的基础上返回布尔列表。

private void UpdateColumns(object c)
{
        if (c == null)
            return;

        var cols = ((IEnumerable)c).Cast<string>().ToList();            

        IsName = cols.Contains("Name") ? true : false;
        IsStatus = cols.Contains("Status") ? true : false;
        IsSize = cols.Contains("Size") ? true : false;
        IsHeight = cols.Contains("Height") ? true : false;
        IsWidth = cols.Contains("Width") ? true : false;
        IsImageType = cols.Contains("Image Type") ? true : false;
        IsFileName = cols.Contains("File Name") ? true : false;
        IsCreationDate = cols.Contains("Creation Date") ? true : false;
        IsEffectiveDate = cols.Contains("Effective Date") ? true : false;
        IsEndDate = cols.Contains("End Date") ? true : false;
        IsDescription = cols.Contains("Description") ? true : false;
}

2 个答案:

答案 0 :(得分:0)

不确定如何使用bool控制可见性。但是带参数的转换器可能有所帮助。

public class ViewModel
    {
        public ViewModel()
        {
            Columns = new List<string>
            {
                "Width", "Height"
            };
        }
        public List<string> Columns { get; private set; }
    }

public class CustomizedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            List<string> list = value as List<string>;
            string keyword = parameter as string;
            return list.Contains(keyword);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

<StackPanel>
        <TextBox Text="Width" IsEnabled="{Binding Columns, Converter={StaticResource CustomizedConverter}, ConverterParameter='Width'}" />
        <TextBox Text="Height" IsEnabled="{Binding Columns, Converter={StaticResource CustomizedConverter}, ConverterParameter='Height'}" />
        <TextBox Text="None" IsEnabled="{Binding Columns, Converter={StaticResource CustomizedConverter}, ConverterParameter='None'}" />
    </StackPanel>

答案 1 :(得分:0)

Yes thats works for me . Thanx a lot. I have created the converter to set the visibility of the columns



public class ColumnVisibilityConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                {
                    List<string> list = value as List<string>;
                    string colName = parameter as string;
                    var visibility = list.Contains(colName) ? Visibility.Visible : Visibility.Collapsed;
                    return visibility;
                }
                public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                {
                    throw new NotImplementedException();
                }
            }


and in xaml i have binded like this

<DataGridTextColumn Header="Name" MinWidth="200" Binding="{Binding Name}" CanUserResize="True"  Visibility="{Binding Data.Columns,Source={StaticResource proxy},Converter={StaticResource ColumnVisibilityConverter}, ConverterParameter='Name'}"/>
<DataGridTextColumn Header="Status" Binding="{Binding Status}" MinWidth="140" CanUserResize="True" Visibility="{Binding Data.Columns,Source={StaticResource proxy},Converter={StaticResource ColumnVisibilityConverter}, ConverterParameter='Status'}" />
<DataGridTextColumn Header="File Name" Binding="{Binding FileName}" MinWidth="150" CanUserResize="True" Visibility="{Binding Data.Columns,Source={StaticResource proxy},Converter={StaticResource ColumnVisibilityConverter}, ConverterParameter='File Name'}" />
<DataGridTextColumn Header="Size" Binding="{Binding ImgSize}" MinWidth="100" CanUserResize="True" Visibility="{Binding Data.Columns,Source={StaticResource proxy},Converter={StaticResource ColumnVisibilityConverter}, ConverterParameter='Size'}" />
<DataGridTextColumn Header="Height" Binding="{Binding Height}" MinWidth="100" CanUserResize="True" Visibility="{Binding Data.Columns,Source={StaticResource proxy},Converter={StaticResource ColumnVisibilityConverter}, ConverterParameter='Height'}" />
                    <DataGridTextColumn Header="Width" Binding="{Binding Width}" MinWidth="100" CanUserResize="True" Visibility="{Binding Data.Columns,Source={StaticResource proxy},Converter={StaticResource ColumnVisibilityConverter}, ConverterParameter='Width'}" />


and in view model i have updated the columns according to the selecting list

private void UpdateColumns(object c)
{
           if (c == null)
                return;

            var cols = ((IEnumerable)c).Cast<string>().ToList();

            Columns.Clear();
            Columns.AddRange(cols);
            RaisePropertyChanged(() => Columns);
}