首页 > 解决方案 > WPF 捕获使用 Binding XPath 填充的标签的内容

问题描述

我只是想获取标签的内容并将其放入变量中。这是我的 Window.xaml

<Grid>
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
            <Label Style="{Binding Source={StaticResource studyTitle}}">Study:</Label>
            <Label Style="{Binding Source={StaticResource studyTitle}}" Content="{Binding XPath=@Name}" Name="Study_Name_Ref"></Label>
        </StackPanel>
</Grid>

这是我的 Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using APPIL_Importer.Methods;
using APPIL_Importer.DicomMethods;

namespace APPIL_Importer
{
    /// <summary>
    /// Interaction logic for StudyImporter.xaml
    /// </summary>
    public partial class StudyImporter : Window
    {
        ConsoleOutputStream outputter;
        public StudyImporter()
        {
            InitializeComponent();
            this.SizeToContent = SizeToContent.WidthAndHeight;

            outputter = new ConsoleOutputStream(TestBox);
            Console.SetOut(outputter);

        }

        public StudyImporter(object data) : this()
        {
            this.DataContext = data;
            this.SizeToContent = SizeToContent.WidthAndHeight;
        }

        private void Confirm_Import_On_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("This will import every series in the Import Directory. Are you sure you want to Import?", "Import Confirmation", MessageBoxButton.OKCancel);
            string study_name_container = Study_Name_Ref.Content.ToString();

            switch (result)
            {
                case MessageBoxResult.OK:
                    Console.WriteLine("Welcome Dave. Would you like to play a game?");
                    MessageBox.Show(study_name_container);
                    //ModifyDicom.ModifyCommonDicomTags(path_to_file);
                    break;
                case MessageBoxResult.Cancel:
                    MessageBox.Show("CANCEL", "Nope!");
                    break;
            }
        }
    }
}

我想捕获名为“Study_Name_Ref”的标签的内容,但我得到的是以下内容:System.Xml.XmlAttribute

有什么建议么?

标签: c#wpflabel

解决方案


标签可以接收对象作为内容。

显然,在您的情况下,标签收到了 System.Xml.XmlAttribute 的实例

那也许你可以试试

XmlAttribute attr = Study_Name_Ref.Content as XmlAttribute;

if(attr != null)
{
   string study_name_container = attr.Value;
}

推荐阅读