Reputation: 2363
I have a window set up that shows the data in an xml file and allows the user to change it, save, etc. I have pretty much evertything set up except that I would like to have a textbox or textblock that shows the file path of the current file that the user is working with.
I have an XmlDataProvider set up called 'xmlData' and I have the text binding as follows:
Text="{Binding Path=Source, Source={StaticResource xmlData}}"
I have also tried the datacontext to the static resource above and both just give me a blank textbox even though I know there is an xml file in the data provider (I can write to it). Am I missing something or can this not be done this way?
Upvotes: 0
Views: 504
Reputation: 2363
Ok well I figured something out that works but it's not what I wanted. I ended up going 'old school' with it. I put a standard property on my form in the code behind with a backing variable and get/set methods. In the set method I just set the text property to the same value as the backing variable.
I'm waiting to mark this as the answer to see if someone has a better solution.
--What I tried--
After the above answer/conversation I tried sticking a converter in that simply returned the value back so that I could break into it in the debugger and see what was being passed to the converter. No matter what I did I couldn't get it to hit the breakpoint so it seems like it wasn't even getting to the converter for some reason.
I then tried to define a dependency property on the code behind of the form itself and a standard property as a wrapper (where the get and set just use the GetValue and SetValue on the dependency property) and bind the textbox text to that using relativesource and findancestor. Basically I bound the text to that property of the form. Then I changed the property using the wrapper in code behind whenever the user browsed for a new file. This worked on the load up, but whenever I set the value in code it didn't update the text in the textbox. I even set the binding tracing to high using diagnostics; I saw that it found the form and said it bound to the value, etc. but whenever I hit the browse method and change the path I get no output and no change in the text.
Like I said, what I have is working and it's reasonable I guess, but if anyone can add insight as to why this wouldn't work I would be very interested to know. I am fairly new to WPF so I want to learn to do these things right...
Upvotes: 0
Reputation: 19842
The problem is that XmlDataProvider.Source
is a Uri
not a string. So you will want to do something like:
Text="{Binding Path=Source.AbsolutePath, Source={StaticResource xmlData}}"
Absolute path isn't going to give you something like: C:\My documents\test.xml
however, it's more likely to be something like: file://c:/my documents/test.xml
. So you'll need to find a good way to manipulate the source Uri
to get what you want.
One thing you could do is use a Converter to parse it and return what you want.
It looks like Uri.LocalPath
might also give you what you want, but I'm not 100% sure without testing it.
Upvotes: 1