简体   繁体   中英

XMLDataprovider label binding codebehind WPF c#

I am working on a WPF application. I set up a multilanguage with xml files and I use static resource binding in front code to set the corrisponding text. 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
  2. You're providing a different XPath to the XmlDataProvider (MenuTextClimate/@Header instead of WpfApplication).
  3. You're also providing a different XPath in the binding expression.

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. 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. If you're on .NET 3.5 sp1 or earlier, this is usually on by default for Error level logging of data binding messages. (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. You turn it on with the 'options' dialog in Visual Studio - it's under Debugging -> Output Window. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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