Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
无法选择wpf列表框项目 我正在创建一个发送API调用并将结果保存到数据库的应用程序。为了避免每次都回到API文档中,我创建了一个小应用程序来提醒我调用的基本元素。它的工作原理是将xml文档加载到内存中,并使用列表框选择适当的调用。然后它的元素显示在文本块(和文本框)中_Wpf_Listboxitem - Fatal编程技术网

无法选择wpf列表框项目 我正在创建一个发送API调用并将结果保存到数据库的应用程序。为了避免每次都回到API文档中,我创建了一个小应用程序来提醒我调用的基本元素。它的工作原理是将xml文档加载到内存中,并使用列表框选择适当的调用。然后它的元素显示在文本块(和文本框)中

无法选择wpf列表框项目 我正在创建一个发送API调用并将结果保存到数据库的应用程序。为了避免每次都回到API文档中,我创建了一个小应用程序来提醒我调用的基本元素。它的工作原理是将xml文档加载到内存中,并使用列表框选择适当的调用。然后它的元素显示在文本块(和文本框)中,wpf,listboxitem,Wpf,Listboxitem,文件“apicalls.xml”的结构如下所示: <Calls> <Call> <Description>blah blah</Description> <api>POST /api/something/somethingElse/etc</api> <Accept>application/xml</Accept> <ContentType>applicat

文件“apicalls.xml”的结构如下所示:

<Calls>
<Call>
    <Description>blah blah</Description>
    <api>POST /api/something/somethingElse/etc</api>
    <Accept>application/xml</Accept>
    <ContentType>application/xml</ContentType>
    <Parameters>id</Parameters>
    <Method>POST</Method>
    <ReceiveFile>true</ReceiveFile>
    <Category>aCategory</Category>
</Call> </Calls>

废话
POST/api/something/somethingElse/etc
应用程序/xml
应用程序/xml
身份证件
邮递
真的
分类
有很多“呼叫”元素。因此,C代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml;
using System.Windows.Threading;

namespace ShowAPI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public XmlDocument doc = new XmlDocument();

    public MainWindow()
    {
        InitializeComponent();
        doc.Load("apicalls.xml");
    }

    private void getAll(string name)
    {
        XmlNodeList nodeList = doc.DocumentElement.SelectNodes("Call");
        int i = 0;
        foreach (XmlNode node in nodeList)
        {
            XmlNode cat = node.SelectSingleNode("Category");
            if (cat.InnerText == name)
            {
                ListBoxItem item = new ListBoxItem();
                item.Content = (++i).ToString() + " " + node.SelectSingleNode("api").InnerText;
                //item.Content = node.SelectSingleNode("api").InnerText;
                item.Name = "N" + i.ToString();
                list.Items.Add(item);
            }

        }
    }

    private void RadioButton_Checked(object sender, RoutedEventArgs e)
    {
        string name = (((RadioButton)sender).Content.ToString());
        list.Items.Clear();
        getAll(name);

        dispatch(); // this is to display the items after the listbox has been populated

    }

    private void dispatch()
    {
        list.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
    }


    private static Action EmptyDelegate = delegate () { };


    private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (list.Items.Count == 0)
            return;

        int index = list.SelectedIndex;
        object sel = e.AddedItems[0];
        string s = sel.ToString();
        XmlNodeList nodes = doc.DocumentElement.SelectNodes("Call");

        IEnumerable<RadioButton> enumerable = panel.Children.OfType<RadioButton>();
        Func<RadioButton, bool> func = (x) => x.IsChecked == true;
        RadioButton res = enumerable.Single<RadioButton>(func);

        string find = res.Content.ToString();
        List<XmlNode> nodeList = new List<XmlNode>();
        foreach (XmlNode xnode in nodes)
        {
            XmlNode api_node = xnode.SelectSingleNode(".//api");
            XmlNode cat_node = xnode.SelectSingleNode(".//Category");
            string s_api = api_node.InnerText;
            string s_cat = cat_node.InnerText;

            if (s_cat == find)
            {
                nodeList.Add(xnode);
            }

        }

        XmlNode xxx = nodeList[index];
        description.Text = xxx.SelectSingleNode(".//Description").InnerText;
        api.Text = xxx.SelectSingleNode(".//api").InnerText.TrimStart(new char[] { 'G', 'E', ' ', 'T', 'L', 'U', 'P', 'S', 'O', 'D' });
        accept.Text = xxx.SelectSingleNode(".//Accept").InnerText;
        contentType.Text = xxx.SelectSingleNode(".//ContentType").InnerText;
        method.Text = xxx.SelectSingleNode(".//Method").InnerText;
        expectFile.Text = xxx.SelectSingleNode(".//ReceiveFile").InnerText;
        parameters.Text = xxx.SelectSingleNode(".//Parameters").InnerText;


    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows;
使用System.Windows.Controls;
使用System.Xml;
使用System.Windows.Threading;
名称空间ShowAPI
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
public XmlDocument doc=新的XmlDocument();
公共主窗口()
{
初始化组件();
doc.Load(“apicalls.xml”);
}
私有void getAll(字符串名称)
{
XmlNodeList nodeList=doc.DocumentElement.SelectNodes(“调用”);
int i=0;
foreach(节点列表中的XmlNode节点)
{
XmlNode cat=node。选择SingleNode(“类别”);
如果(cat.InnerText==名称)
{
ListBoxItem=新建ListBoxItem();
item.Content=(++i.ToString()+“”+node.SelectSingleNode(“api”).InnerText;
//item.Content=node.SelectSingleNode(“api”).InnerText;
item.Name=“N”+i.ToString();
列表.项目.添加(项目);
}
}
}
已选中专用无效单选按钮(对象发送器,路由目标e)
{
字符串名称=(((RadioButton)sender.Content.ToString());
list.Items.Clear();
getAll(姓名);
dispatch();//这是在填充列表框后显示项目
}
私密
{
list.Dispatcher.Invoke(DispatcherPriority.Render,EmptyDelegate);
}
私有静态操作EmptyDelegate=delegate(){};
私有无效列表\u选择已更改(对象发送者,选择已更改已更改)
{
if(list.Items.Count==0)
返回;
int index=list.SelectedIndex;
对象sel=e.AddedItems[0];
字符串s=sel.ToString();
XmlNodeList nodes=doc.DocumentElement.SelectNodes(“调用”);
IEnumerable enumerable=panel.Children.OfType();
Func Func=(x)=>x.IsChecked==true;
RadioButton res=可枚举单(func);
字符串find=res.Content.ToString();
列表节点列表=新列表();
foreach(XmlNode中的xnode)
{
XmlNode api_node=xnode.SelectSingleNode(“.//api”);
XmlNode cat_node=xnode.SelectSingleNode(“.//类别”);
字符串s_api=api_node.InnerText;
字符串s_cat=cat_node.InnerText;
如果(s_cat==查找)
{
nodeList.Add(xnode);
}
}
XmlNode xxx=节点列表[索引];
description.Text=xxx.SelectSingleNode(“.//description”).InnerText;
api.Text=xxx.SelectSingleNode(“.//api”).InnerText.TrimStart(新字符[]{'G','E','T','L','U','P','S','O','D'});
accept.Text=xxx.SelectSingleNode(“.//accept”).InnerText;
contentType.Text=xxx.SelectSingleNode(“.//contentType”).InnerText;
method.Text=xxx.SelectSingleNode(“.//方法”).InnerText;
expectFile.Text=xxx.SelectSingleNode(“.//ReceiveFile”).InnerText;
parameters.Text=xxx.SelectSingleNode(“.//参数”).InnerText;
}
}
}

这是xaml中的ListBox实现:

<ListBox Name ="list" HorizontalAlignment="Left" Height="396" Margin="582,52,0,0" VerticalAlignment="Top" Width="561" SelectionChanged="list_SelectionChanged">
        <ListBox.Background>
            <RadialGradientBrush Center="0.1,0.5" GradientOrigin="2,1" RadiusX="1" RadiusY="0.3" SpreadMethod="Reflect">
                <GradientStop Color="#FFC7431C" Offset="1"/>
                <GradientStop Color="#FFD31010" Offset="0.494"/>
            </RadialGradientBrush>
        </ListBox.Background>
    </ListBox>c

C
因此,列表框是正常填充的,我可以看到所有项目,但当我将鼠标移到它们上面时,任何超过12的项目都无法被选择(它们似乎不是交互式的-移动到项目上时背景不会改变)

我曾尝试将listboxitems作为对象而不是文本插入(我在某个地方读过),并通过在实际内容之前添加索引号和方法名来避免插入相同的项。似乎什么都不管用,我也不知道

另外,我还使用反射来获取properties(),并将第一个listboxitem的值与第十四个listboxitem的值进行比较,它们除了名称和内容之外是相同的。
我刚刚注意到用箭头键选择项目效果很好。鼠标选择在12日之后对任何项目都不起作用。

我建议您根据WPF的核心功能(如
XmlDataProvider
和当前项目同步)使用稍微不同的方法。
因此,首先在窗口资源中定义XmlDataProvider:

<Window.Resources>
    <XmlDataProvider x:Key="CallsData">
        <x:XData>
            <Calls xmlns=''>
                <Call>
                    <Description>blah blah 3</Description>
                    <api>POST /api/something</api>
                    <Accept>application/xml</Accept>
                    <ContentType>application/xml</ContentType>
                    <Parameters>id</Parameters>
                    <Method>POST</Method>
                    <ReceiveFile>true</ReceiveFile>
                    <Category>Cat1</Category>
                </Call>
                <Call>
                    <Description>blah blah 2</Description>
                    <api>POST /api/something/somethingElse</api>
                    <Accept>application/xml</Accept>
                    <ContentType>application/xml</ContentType>
                    <Parameters>id</Parameters>
                    <Method>POST</Method>
                    <ReceiveFile>true</ReceiveFile>
                    <Category>Cat3</Category>
                </Call>
                <Call>
                    <Description>blah blah 3</Description>
                    <api>POST /api/something/somethingElse/etc</api>
                    <Accept>application/xml</Accept>
                    <ContentType>application/xml</ContentType>
                    <Parameters>id</Parameters>
                    <Method>POST</Method>
                    <ReceiveFile>true</ReceiveFile>
                    <Category>Cat2</Category>
                </Call>
            </Calls>
        </x:XData>
    </XmlDataProvider>
</Window.Resources>

胡说八道
POST/api/something
应用程序/xml
应用程序/xml
身份证件
邮递
真的
第一类
废话
POST/api/某物/某物
应用程序/xml
应用程序/xml
身份证件
邮递
真的
第三类
胡说八道
POST/api/something/somethingElse/etc
应用程序/xml
应用程序/xml
身份证件
邮递
真的
第二类
<DockPanel LastChildFill="True" 
           DataContext="{Binding Source={StaticResource CallsData}, XPath=/Calls/Call}">
    <StackPanel DockPanel.Dock="Bottom">
        <TextBlock Text="{Binding XPath=Description, StringFormat='Description = {0}'}"/>
        <TextBlock Text="{Binding XPath=api, StringFormat='api = {0}'}"/>
        <TextBlock Text="{Binding XPath=Accept, StringFormat='Accept = {0}'}"/>
        <TextBlock Text="{Binding XPath=ContentType, StringFormat='ContentType = {0}'}"/>
        <TextBlock Text="{Binding XPath=Parameters, StringFormat='Parameters = {0}'}"/>
        <TextBlock Text="{Binding XPath=Method, StringFormat='Method = {0}'}"/>
        <TextBlock Text="{Binding XPath=ReceiveFile, StringFormat='ReceiveFile = {0}'}"/>
        <TextBlock Text="{Binding XPath=Category, StringFormat='Category = {0}'}"/>
    </StackPanel>
    <ListBox Name="list" 
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding XPath=Category}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DockPanel>