Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 复选框呈现为标签_C#_Wpf_Listview_Checkbox_Mvvm - Fatal编程技术网

C# 复选框呈现为标签

C# 复选框呈现为标签,c#,wpf,listview,checkbox,mvvm,C#,Wpf,Listview,Checkbox,Mvvm,我有一个动态生成的ListView,它使用数据绑定允许通过复选框编辑一些布尔值。我使用IValueConverter生成ListView的列(如回答中所示): 在XAML中也是这样使用的: <ListView Margin="2" ItemContainerStyle="{StaticResource LineHighlightListView}" ItemsSource="{Binding InMatrixList}" View="

我有一个动态生成的ListView,它使用数据绑定允许通过复选框编辑一些
布尔值。我使用IValueConverter生成ListView的列(如回答中所示):

在XAML中也是这样使用的:

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

列的标题在别处生成。代码应该接受
ColumnConfig
项,并创建
GridViewColumn
对象,这些对象的
ChechBox
数据绑定到其他地方的某个值。然而,我得到的只是用文本代替复选框的列。文本正确,因此数据绑定有效,但
FrameworkElementFactory
对象未按预期工作


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

规则:避免以这种方式动态组合模板

我有一个类似的问题,我已解决如下:

    //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;
    }
//请参见:http://www.codeproject.com/Articles/444371/Creating-WPF-Data-Templates-in-Code-The-Right-Way
专用静态数据模板CreateTemplate(UniprogCellVM单元)
{
var tcell=cell.GetType();
var sb=新的StringBuilder();
sb.AppendFormat(“,tcell.Name”);
某人加上(“”);
某人加上(“”);
某人加上(“”);
var context=new ParserContext();
context.XamlTypeMapper=新的XamlTypeMapper(新字符串[0]);
context.XamlTypeMapper.AddMappingProcessingInstruction(“local”,tcell.Namespace,tcell.Assembly.FullName);
context.xmlnsdirectionary.Add(“,”http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.xmlnsdirectionary.Add(“x”http://schemas.microsoft.com/winfx/2006/xaml");
Add(“local”、“local”);
var template=(DataTemplate)XamlReader.Parse(sb.ToString(),context);
返回模板;
}
基本上,您应该为模板编写一个完全有效的XAML,然后使用解析器对其进行解析

由于文本合成是一项简单的任务,您可以在creation函数中传递任何参数(如上面的示例所示)


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

这是一种非常糟糕的方法。使用MVVM和数据模板instead@GlenThomas我已经在使用MVVM了。我不认为有一种方法可以正确地使用数据模板,是吗?DataTemplate将应用于所有单元格,包括第一列,它是纯文本的。您是否觉得自己陷入了一个兔子洞?因为你要进一个兔子洞。创建一个自定义控件或usercontrol,该控件根据绑定对象的状态在codebehind中操纵UI。@Will在尝试该解决方案后,如果不执行上述大部分操作,我仍然看不出您会如何操作—唯一的变化是我在codebehind中操作,而不是在转换器中操作……嗯,这是一个问题(该表将有约100个元素,并且将有一些表使用此系统)。我想我会听从@will的建议,不再深入兔子洞了。我将只做一个UserControl,在代码隐藏中处理这个问题。100元素根本不是问题。我将这种方法用于一个拥有数百个单元格(对我来说这是一个“元素”)的超级专用网格,其中大部分都有一个相当复杂的UI。我的意思是,对于成千上万的单元格,应该避免这种方法。不,这是不必要的,因为上面的函数生成了一个DataTemplate实例,该实例直接注入ContentControl。当然,您可以预先计算(即编译)模板,而应用程序将面临定义良好的布局。就我而言,它不是。但是,请尝试一个测试程序,以了解如何执行此方法。
    //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;
    }