mardi 26 avril 2016

CheckBox rendered as a Label

I have a dynamically generated ListView, that uses data binding to allow editing some Boolean values through CheckBox. I use a IValueConverter to generate the ListView's columns (like in this answer):

public object Convert (object Value, Type EntryType, object Parameter, System.Globalization.CultureInfo Culture)
{
    var Config = Value as ColumnConfig;
    if (Config != null)
    {
        var GridView = new GridView ();
        Binding NameBinding = new Binding ("Name");
        GridViewColumn BaseColumn = new GridViewColumn { Header = "Settings", 
                                                       DisplayMemberBinding = NameBinding,
                                                       Width = 125,
                                                       CellTemplate = new DataTemplate ()};
        GridView.Columns.Add (BaseColumn);

        foreach (Column CurrentColumn in Config.Columns)
        {
            Binding NewBinding = new Binding (CurrentColumn.DataField);
            FrameworkElementFactory FEF = new FrameworkElementFactory (typeof (CheckBox));
            FEF.SetBinding (CheckBox.IsCheckedProperty, NewBinding);

            GridViewColumn GVColumn = new GridViewColumn
                                            {
                                              Header = CurrentColumn.Header,
                                              DisplayMemberBinding = NewBinding
                                            };
            var DTemplate = new DataTemplate ();
            DTemplate.VisualTree = FEF;

            GVColumn.CellTemplate = DTemplate;

            GridView.Columns.Add (GVColumn);
        }

        return GridView;
    }

    return Binding.DoNothing;
}

Which is used like so in the XAML:

<ListView Margin="2" ItemContainerStyle="{StaticResource LineHighlightListView}"
              ItemsSource="{Binding InMatrixList}"
              View="{Binding InMatrixColumns, Converter={StaticResource ConvertItemsToDynamicGridView}}" />

The columns' headers are generated elsewhere. The code should take in a ColumnConfig items, and create GridViewColumn objects that have a ChechBox databound to some other value elsewhere. However, all I am getting is columns with text in place of the CheckBoxes. The text is correct, so the data binding is valid, but the FrameworkElementFactory object is not working as expected.

Why are the checkboxes rendered/converted to textboxes?




Aucun commentaire:

Enregistrer un commentaire