DataTemplate绑定复选框

时间:2018-07-18 14:01:54

标签: c# wpf data-binding datatemplate dynamic-columns

我的问题:

我需要通过代码设置我的checkboxex的databinind,但是那些Checkboxes都已放入DataTemplate中以应用于具有动态列的列表视图,但我确实可以使用,但是我不喜欢这样做的方式,因为它打破了我在Listview中的动态列概念,现在我为Listview中的每个可能的列创建一个DataTemplate,所有动态列都将是CheckBoxes,所以我猜想我只能创建一个DataTempleate的方式,而且不多,现在

我的代码:

我有一个这样的班级:

public class CArea
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool[] items { get; set; }

    public CArea()
    {
        items = new bool[3]; //this is the dynamic,every field will be a column
        items[0] = true;
        items[1] = true;
        items[2] = false;
    }
}

我需要用动态列绑定ListView中的ObservableCollection Areas,在此示例中为3个项目,但可以为4,2,5或其他任何值,使用转换器可以动态生成列问题(感谢{{{3 }}),我的问题是Areas集合中每个项目的CheckBox绑定,现在我正在编写一个xaml,像这样

<Window.Resources>
        <!--<datagridDinamico:ConfigToDynamicGridViewConverter x:Key="ConfigToDynamicGridViewConverter" />-->
        <local:ConfigToDynamicGridViewConverter x:Key="ConfigToDynamicGridViewConverter" />
        <DataTemplate x:Key="Chk0" DataType="{x:Type GridViewColumn}">
            <CheckBox IsChecked="{Binding items[0]}" />
        </DataTemplate>
        <DataTemplate x:Key="Chk1" DataType="{x:Type GridViewColumn}">
            <CheckBox IsChecked="{Binding items[1]}" />
        </DataTemplate>
        <DataTemplate x:Key="Chk2" DataType="{x:Type GridViewColumn}">
            <CheckBox IsChecked="{Binding items[2]}" />
        </DataTemplate>

    </Window.Resources>
<ListView ItemsSource="{Binding Areas}" View="{Binding ColumnConfig, Converter={StaticResource ConfigToDynamicGridViewConverter}}"/>

每个items[i]都有一个模板,但是它很糟糕,破坏​​了列表视图的动态概念。转换器代码是这样的:

public class ConfigToDynamicGridViewConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var config = value as ColumnConfig;
            if (config != null)
            {
                var grdiView = new GridView();
                foreach (var column in config.Columns)
                {
                    bool usaTemplate = !string.IsNullOrEmpty(column.ContentControlDataTemplate);
                    var binding = new Binding(usaTemplate ? column.ContentControlDataTemplate : column.TextDataField);
                    if (usaTemplate)
                    {
                        Window window = Application.Current.MainWindow;
                        DataTemplate template = (DataTemplate)window.FindResource(column.ContentControlDataTemplate);
                        grdiView.Columns.Add(new GridViewColumn { Header = column.Header, CellTemplate = template });

                    }
                    else
                        grdiView.Columns.Add(new GridViewColumn { Header = column.Header, DisplayMemberBinding = binding });
                }
                return grdiView;
            }
            return Binding.DoNothing;
        }



        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

如您所见,我从列config对象中获取了DataTemplate,...这并不是很好,因为我需要像这样在viewmodel中手动进行设置

public class ViewModel
    {
        public ColumnConfig ColumnConfig { get; set; }
        public ObservableCollection<CArea> Areas { get; set; }
        public ViewModel()
        {
            Areas = new ObservableCollection<CArea>() {
                new CArea {Id=1, Name = "Area 1" },
                new CArea(){Id=2,Name="Area2" } };
            ColumnConfig = new ColumnConfig
            {
                Columns = new List<Column>
                {
                    new Column { Header = "Area",   TextDataField = "Nombre" }
                }
            };
            Categorias = ServCategory.GetAllPossibleColums();
            int i = 0;
            foreach (var c in Categorias)
            {
                ColumnConfig.Columns.Add(new Column() { Header = c.Nombre, ContentControlDataTemplate = "Chk"+i.ToString()});

                i++;
            }
        }

        private List<CCategoria> categorias;
        public List<CCategoria> Categorias
        {
            get { return categorias; }
            set { categorias = value; }
        }
}

仅此而已,希望您能为我提供帮助

谢谢您的时间

0 个答案:

没有答案