在 WPF 中使用 XML 属性作为列表源

发布于 2024-12-12 14:53:02 字数 1198 浏览 0 评论 0原文

我需要能够使用一系列属性作为 WPF 列表视图中的源。我目前陷入了如何做到这一点的困境。我知道如何对 XML 节点执行此操作,但不知道如何对属性执行此操作。这是我也尝试绑定的示例:

<Stats HP="55000" MP="2500" SP="2500" Strength="212" Vitality="125" Magic="200" Spirit="162" Skill="111" Speed="109" Evasion="100" MgEvasion="100" Accuracy="100" Luck="55" />

我希望每个统计名称/值都是列表视图中的一个条目。下面是我一直在尝试实现的代码:

<TabControl.DataContext>
     <Binding ElementName="EnemyLevelsListBox" Path="SelectedItem"/>
</TabControl.DataContext>
<TabItem Header="Stats" Name="EnemyStatsTab">
      <ListView ItemsSource="{Binding XPath=Stats}" Height="310" Name="EnemyStatViewDisplay" Width="411" HorizontalAlignment="Left">
      <ListView.ItemTemplate>
            <DataTemplate>
                  <TextBlock Text="{Binding Path=Attribute.Name}" />
                  <TextBlock Text="{Binding Path=Attribute.Value}" />
            </DataTemplate>
       </ListView.ItemTemplate>
       </ListView>
</TabItem>

TabControl.DataContext 绑定公开了 Stats 元素的父 XMLNode。我肯定知道这一点,因为我在其他地方使用父节点的子节点并且它们工作正常。我不知道要为 ListView ItemsSource 或 DataTemplate 的文本块绑定放置什么。有人知道该怎么做吗?

I need to be able to use a series of attributes as the source in a WPF list view. I'm currently stuck on how to do this. I know how to do it for XML nodes but not for attributes. Here's an example of what I'm trying to bind too:

<Stats HP="55000" MP="2500" SP="2500" Strength="212" Vitality="125" Magic="200" Spirit="162" Skill="111" Speed="109" Evasion="100" MgEvasion="100" Accuracy="100" Luck="55" />

I want each stat name/value to be an entry in the listview. Here's the code I've been trying to make work:

<TabControl.DataContext>
     <Binding ElementName="EnemyLevelsListBox" Path="SelectedItem"/>
</TabControl.DataContext>
<TabItem Header="Stats" Name="EnemyStatsTab">
      <ListView ItemsSource="{Binding XPath=Stats}" Height="310" Name="EnemyStatViewDisplay" Width="411" HorizontalAlignment="Left">
      <ListView.ItemTemplate>
            <DataTemplate>
                  <TextBlock Text="{Binding Path=Attribute.Name}" />
                  <TextBlock Text="{Binding Path=Attribute.Value}" />
            </DataTemplate>
       </ListView.ItemTemplate>
       </ListView>
</TabItem>

The TabControl.DataContext binding exposes the parent XMLNode of the Stats element. I know this for certain as I'm using subnodes of the parent elsewhere and they work correctly. I don't know what to put for the ListView ItemsSource or for the DataTemplate's textblock binds. Does someone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2024-12-19 14:53:03

您不应在 ItemsSource 绑定中使用 XPath,因为您希望定位 Attributes 集合位于 XmlNode 对象。您应该确保您已经将 Stats 元素作为 DataContext,然后将 Path 设置为 Attributes

<ListView ItemsSource="{Binding Attributes}" DataContext="<bind to stats element if necessary>">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Stat" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Value}"/>
        </GridView>
    </ListView.View>
</ListView>

You should not use XPath in the ItemsSource binding as you want to target the Attributes collection on the XmlNode object. You should make sure you are already having the Stats element as DataContext, then set the Path to Attributes:

<ListView ItemsSource="{Binding Attributes}" DataContext="<bind to stats element if necessary>">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Stat" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Value}"/>
        </GridView>
    </ListView.View>
</ListView>
落花随流水 2024-12-19 14:53:03

为什么不直接将列表视图绑定到属性呢?

XAML:

<Window x:Class="WPF1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPF1"
        Title="MainWindow" Height="350" Width="350">

    <StackPanel>
        <StackPanel.Resources>
            <local:AttributesToEnumerableConverter x:Key="AttributesToEnumerableConverter" />
        </StackPanel.Resources>
        <ListView ItemsSource="{Binding Path=Stats, Converter={StaticResource AttributesToEnumerableConverter}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" Margin="5" />
                        <TextBlock Text="{Binding Path=Value}" Margin="5" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>

</Window>

隐藏代码:

using System;
using System.Windows;
using System.Windows.Data;
using System.Xml.Linq;

namespace WPF1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;
        }

        public XElement Stats
        {
            get { return XElement.Parse("<Stats HP=\"55000\" MP=\"2500\" SP=\"2500\" Strength=\"212\" Vitality=\"125\" Magic=\"200\" Spirit=\"162\" Skill=\"111\" Speed=\"109\" Evasion=\"100\" MgEvasion=\"100\" Accuracy=\"100\" Luck=\"55\" />"); }
        }
    }

    public class AttributesToEnumerableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value as XElement).Attributes(); 
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Why not just bind the listview to the attributes?

XAML:

<Window x:Class="WPF1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPF1"
        Title="MainWindow" Height="350" Width="350">

    <StackPanel>
        <StackPanel.Resources>
            <local:AttributesToEnumerableConverter x:Key="AttributesToEnumerableConverter" />
        </StackPanel.Resources>
        <ListView ItemsSource="{Binding Path=Stats, Converter={StaticResource AttributesToEnumerableConverter}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name}" Margin="5" />
                        <TextBlock Text="{Binding Path=Value}" Margin="5" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>

</Window>

Code behind:

using System;
using System.Windows;
using System.Windows.Data;
using System.Xml.Linq;

namespace WPF1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;
        }

        public XElement Stats
        {
            get { return XElement.Parse("<Stats HP=\"55000\" MP=\"2500\" SP=\"2500\" Strength=\"212\" Vitality=\"125\" Magic=\"200\" Spirit=\"162\" Skill=\"111\" Speed=\"109\" Evasion=\"100\" MgEvasion=\"100\" Accuracy=\"100\" Luck=\"55\" />"); }
        }
    }

    public class AttributesToEnumerableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value as XElement).Attributes(); 
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文