比较字符串列表和布尔值列表
本文关键字:列表 布尔值 比较 字符串 | 更新日期: 2023-09-27 18:18:17
目前我正在这样做,以设置这么多布尔属性的可见性。我想要一个通用的方法来接受布尔属性列表和字符串列表,然后返回布尔列表的基础上,如果特定的字符串存在于列表中。
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;
}
不确定如何使用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>
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);
}