CheckBox呈现为标签

时间:2016-04-26 13:35:35

标签: c# wpf listview checkbox mvvm

我有一个动态生成的ListView,它使用数据绑定来允许通过CheckBox编辑一些Boolean值。我使用IValueConverter生成ListView的列(如this中的答案):

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

在XAML中使用的是这样的:

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

列&#39;标头在别处生成。代码应该包含ColumnConfig项,并创建GridViewColumn个对象,其中ChechBox数据绑定到其他地方的某个其他值。但是,我得到的只是带有文本的列代替CheckBoxes。文本是正确的,因此数据绑定有效,但FrameworkElementFactory对象未按预期工作。

为什么复制框会呈现/转换为文本框?

1 个答案:

答案 0 :(得分:1)

规则:避免以这种方式动态撰写模板

我遇到了类似的问题,我解决了以下问题:

    //see: http://www.codeproject.com/Articles/444371/Creating-WPF-Data-Templates-in-Code-The-Right-Way
    private static DataTemplate CreateTemplate(UniprogCellVM cell)
    {
        var tcell = cell.GetType();

        var sb = new StringBuilder();
        sb.AppendFormat("<DataTemplate DataType=\"{{x:Type local:{0}}}\">", tcell.Name);
        sb.Append("<local:UniprogCellControl ");
        sb.Append("Content=\"{Binding Path=.}\" ");
        sb.Append("Header=\"{Binding Path=.}\" ");
        sb.AppendFormat("Style=\"{{DynamicResource Root{0}BoxStyleKey}}\" ", cell.Interaction);
        sb.Append(">");

        sb.Append("</local:UniprogCellControl>");
        sb.Append("</DataTemplate>");

        var context = new ParserContext();

        context.XamlTypeMapper = new XamlTypeMapper(new string[0]);
        context.XamlTypeMapper.AddMappingProcessingInstruction("local", tcell.Namespace, tcell.Assembly.FullName);

        context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
        context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
        context.XmlnsDictionary.Add("local", "local");

        var template = (DataTemplate)XamlReader.Parse(sb.ToString(), context);
        return template;
    }

基本上,你应该编写一个完全有效的模板XAML,然后用解析器解析它。

由于文本合成是一项微不足道的任务,您可以在创建函数中传递任何参数(如上例所示)。

最后一点:这种方法很有用,但需要计算工作量,因为运行时解析和编译。避免以这种方式创建大量项目。