c# - 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
有什么建议么?
解决方案
标签可以接收对象作为内容。
显然,在您的情况下,标签收到了 System.Xml.XmlAttribute 的实例
那也许你可以试试
XmlAttribute attr = Study_Name_Ref.Content as XmlAttribute;
if(attr != null)
{
string study_name_container = attr.Value;
}
推荐阅读
- c# - 在 Windows 应用程序表单中显示 reCAPTCHA
- c++ - std::this_thread::sleep_for 实现取决于系统时钟
- android - 添加外部库后,AndroidManifest.xml 中的应用程序图标问题
- javascript - 无法在 react.js 的模板字符串中呈现 html 标签
- ios - 如何从消息过滤器扩展中获取数据?
- python - 熊猫 - 自上次交易以来的计数
- css - 避免组件在角度上重叠
- c++ - win 将应用程序 String^ 转换为 int
- python - numpy.random.choice() 无法正常工作
- java - 使用带有未经检查分配的 lambda 的 assertThatThrownBy 时找不到符号