-3

現在、私は非常に多くのブール値のプロパティの可視性を設定するためにこのようにしています。ブール型プロパティのリストと文字列のリストを取得し、特定の文字列がリストに存在する場合に基づいてブール型リストを返すジェネリック メソッドが必要です。

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;
}
4

2 に答える 2

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>
于 2013-06-13T12:52:29.450 に答える
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);
}
于 2013-06-14T07:55:27.033 に答える