C# 如何从xml文件目标创建新复选框

C# 如何从xml文件目标创建新复选框,c#,wpf,xml,C#,Wpf,Xml,如果在xml文件中有一个新目标,如 <target name="main" depends="init"> <antcall target="Exec_Install_abc" /></target> 听起来我需要做xml解析。但我不确定这样做是否正确。这意味着我将最终编辑我的xaml和xaml.cs文件。我还需要删除“下划线”,并根据需要在单词之间留出空格。我有点困惑从哪里开始,如何开始。如果有人能引导我出发,我将不胜感激。谢谢 编辑1: 如果单击当前图

如果在xml文件中有一个新目标,如

<target name="main" depends="init">
 <antcall target="Exec_Install_abc" /></target>
听起来我需要做xml解析。但我不确定这样做是否正确。这意味着我将最终编辑我的xaml和xaml.cs文件。我还需要删除“下划线”,并根据需要在单词之间留出空格。我有点困惑从哪里开始,如何开始。如果有人能引导我出发,我将不胜感激。谢谢

编辑1:
如果单击当前图形用户界面上的“刷新”按钮,我将能够在图形用户界面中添加一个复选框。这样可以刷新图形用户界面并显示新的复选框。好吧,我可以告诉你怎么做,但不是完全用代码。因为这将需要大量的代码。我可以告诉你怎么走,下面是怎么走的:

最简单的方法是首先解析xml。您可以为此使用linq到xml

在此之后,可以使用
String.Replace
方法删除下划线并添加空格。比如:

string formattedCaption = checkBoxCaptionExtractedFromXml.Replace('_', ' ');
这将解决如何加载xml的问题。现在,您可以创建一个窗口,并将带有
复选框的
列表视图声明为
GridViewColumn
,该窗口绑定到一个类,该类包含名称和一个bool,以表示
复选框的状态。像这样:

public class MyCheckBoxes
{
    public bool State { get; set; }
    public string Caption { get; set; }
}
您必须用要显示的复选框标题填充此类。您可以这样声明您的
列表视图

<ListView x:Name="myCheckBoxColumns" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding}">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="250" Header="Some Header">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding State}" Content="{Binding Caption}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

我相信这应该对你有好处。如果无法理解,请随时提问。

让我们假设xml存储在位置“c:\test.xml”中,并且包含以下内容:

<?xml version="1.0" standalone="yes"?>
<target name="main" depends="init">
 <antcall target="Exec_Install_abc" />
</target>
已编辑

考虑更复杂的情况。我们的xml文档如下所示:

<?xml version="1.0" standalone="yes"?>
<targets>
    <target name="main" depends="init">
        <antcall target="Exec_Install_abc" />
    </target>
    <target name="main" depends="init">
        <antcall target="Ololo_jesus_christ" />
    </target>
    <target name="main" depends="init">
        <antcall target="One_____more target" />
    </target>
</targets>

现在我们有很多目标,必须创建很多复选框。好的,我们需要ItemsControl。ItemsControl的ItemsSource将是从antcall节点(XmlAttribute对象)中选择的目标属性集合。然后我们定义一个DataTemplate,将复选框放入其中,并通过移除下划线符号的转换器将内容属性绑定到值属性

尽管存在明显的复杂性,但结果代码相当简单:

<ItemsControl ItemsSource="{Binding Source={StaticResource xDoc}, XPath=targets/target/antcall/@target}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Path=Value, Converter={StaticResource UnderscoresConverter}}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


谢谢您的帮助。我现在正在研究你的算法。。如果我遇到问题,我会更新你我只是有一个关于解析的问题。如果让我们假设之前我已经有一些目标,我已经硬编码到复选框中,例如,它也是以我如何确保Exec_Install_Xyz没有编码到复选框中的形式?我有很多这样的目标需要担心,而且我怎么知道这个“Exec_Install_Abc”是巨大xml文件中的一个新目标?你可以制作一个复选框标题的字典,并在将其添加到
列表框之前检查该字典。嗨,yogesh,我可以举一个拥有字典的例子吗?而且。。。我已经有了一个带有一些选项卡项的选项卡控件。我希望将此listview放入选项卡项中。我想这是可能的,对吗?因为现在我所有的标签项都包含一个网格,如果我实现这个列表视图,我会很困惑。我想知道你到底把标签放在哪里了。。。在xaml文件中?哦,是的,顺便说一句,如果我有许多目标要实现到复选框中,这能起作用吗?你说的“许多目标要实现到复选框中”是什么意思。一个复选框应该显示一组目标,或者每个目标应该放在单独的复选框中?@alex zhevzhik如果不清楚,很抱歉。。我的意思是。如果我在xml文件中有许多新的目标要生成,那么上面提到的代码是否能够为每个目标生成一个复选框?表示4个新目标=4个新支票boxhi alex,感谢您的回复。我想我大概知道你的密码了。不过我有这个问题。这里的“self”是一个未声明的名称空间。我有什么遗漏吗?另外,如何设置字典来检查这些目标是否已生成到复选框中?
<Window.Resources>
    <XmlDataProvider Source="c:\\test.xml" x:Key="xDoc"/>
    <self:UnderscoresConverter x:Key="UnderscoresConverter"/>
</Window.Resources>
<CheckBox Content="{Binding Source={StaticResource xDoc}, XPath=target/antcall/@target, Path=Value, Converter={StaticResource UnderscoresConverter}}"/>
public class UnderscoresConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString().Replace('_', ' ');
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<?xml version="1.0" standalone="yes"?>
<targets>
    <target name="main" depends="init">
        <antcall target="Exec_Install_abc" />
    </target>
    <target name="main" depends="init">
        <antcall target="Ololo_jesus_christ" />
    </target>
    <target name="main" depends="init">
        <antcall target="One_____more target" />
    </target>
</targets>
<ItemsControl ItemsSource="{Binding Source={StaticResource xDoc}, XPath=targets/target/antcall/@target}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Path=Value, Converter={StaticResource UnderscoresConverter}}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>