开发者

XMLDataprovider label binding codebehind WPF c#

开发者 https://www.devze.com 2023-01-24 02:02 出处:网络
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

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/@He开发者_如何学Pythonader";
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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号