简体   繁体   English

WPF c#背后的XMLDataprovider标签绑定代码

[英]XMLDataprovider label binding codebehind WPF c#

I am working on a WPF application. 我正在处理WPF应用程序。 I set up a multilanguage with xml files and I use static resource binding in front code to set the corrisponding text. 我使用xml文件设置了多语言,并且在前面的代码中使用了静态资源绑定来设置相应的文本。 The issue I have is with doing the same thing in the codebehind. 我的问题是在后面的代码中做同样的事情。

Here you can see how I use it in front code: 在这里,您可以看到我如何在前面的代码中使用它:

<XmlDataProvider x:Key="Lang" Source="/lng/english.xml" XPath="WpfApplication"/>
<Label HorizontalAlignment="Center" Margin="0,10,0,5" Foreground="White" FontWeight="Bold" Content="{Binding Source={StaticResource Lang}, XPath=MenuTextClimate/@Header}"></Label>

I am trying to do the same in codebehind like this: 我试图像这样在代码隐藏中做同样的事情:

String selLangFullPath = WpfLibrary.LanguageOptions.getSelLangFullPath();
XmlDataProvider xmlData = (XmlDataProvider)(this.FindResource("Lang"));
xmlData.Source = new Uri(selLangFullPath, UriKind.Relative);
xmlData.XPath = "MenuTextClimate/@Header";
Binding NewBinding = new Binding();
NewBinding.Source = xmlData;
NewBinding.Mode = BindingMode.OneWay;
NewBinding.XPath = "MenuTextClimate";
lblTitle.SetBinding(Label.ContentProperty, NewBinding);

but for some reason it doesent seem to work. 但由于某种原因,它确实起作用了。 Can any one tell me where I went wrong? 谁能告诉我我哪里出问题了?

Thanks in advanced. 提前致谢。

The codebehind you've shown doesn't actually do the same thing. 您所显示的代码实际上并没有做同样的事情。 It's different in three ways: 它在三个方面有所不同:

  1. You're changing the Source property of the XmlDataProvider 您正在更改XmlDataProvider的Source属性
  2. You're providing a different XPath to the XmlDataProvider (MenuTextClimate/@Header instead of WpfApplication). 您正在为XmlDataProvider提供不同的XPath(MenuTextClimate / @ Header而不是WpfApplication)。
  3. You're also providing a different XPath in the binding expression. 您还将在绑定表达式中提供其他XPath。

The problem could simply be that any or all of those things is wrong. 问题可能仅仅是这些东西中的任何一个或全部是错误的。 (The XPath ones look particularly suspicious, because they look like they presume a completely different XML document structure. Although since you're also providing a different XML document, maybe that's fine. It's impossible to tell from the information provided so far.) So the first thing I'd do is try making your C# do exactly the same as your Xaml - same URI and same XPaths. (XPath看起来特别可疑,因为它们看起来好像假定了一个完全不同的XML文档结构。尽管由于您还提供了一个不同的XML文档,所以也许还可以。从目前提供的信息中看不出来。)我要做的第一件事是尝试使您的C#与Xaml完全相同-相同的URI和相同的XPath。 If that works, it should be easier to see which of the three things that's different is causing the problem. 如果这样可行,那么应该更容易了解导致问题的三件事中哪一项不同。

Alternatively, enable WPF debug output. 或者,启用WPF调试输出。 If you're on .NET 3.5 sp1 or earlier, this is usually on by default for Error level logging of data binding messages. 如果您使用的是.NET 3.5 sp1或更早版本,则通常默认情况下启用此级别,以记录数据绑定消息的错误级别。 (Data binding errors appear in the Output window.) As of .NET 4.0, Microsoft turned it down so you won't see it unless you ask for it. (数据绑定错误出现在“输出”窗口中。)从.NET 4.0开始,Microsoft拒绝了它,因此除非您提出要求,否则您将看不到它。 You turn it on with the 'options' dialog in Visual Studio - it's under Debugging -> Output Window. 您可以使用Visual Studio中的“选项”对话框将其打开,它位于“调试”->“输出窗口”下。 Ensure that Data Binding is set to show errors. 确保将数据绑定设置为显示错误。 Or for more detail, crank it all the way up and then enable full logging by adding this: 或者,有关更多详细信息,请将其完全启动,然后通过添加以下内容来启用完整日志记录:

PresentationTraceSources.SetTraceLevel(NewBinding, PresentationTraceLevel.High);

That should show you full gory details of what data binding is attempting to do with your binding, and that's often a pretty good way to find out why things aren't working. 那应该向您显示有关数据绑定正在试图与绑定进行连接的详细信息,并且这通常是找出为什么事情不起作用的一种很好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM