WPF - Bind a ListView to a .xml file dynamically - c#

Is there a way (using the MVVM pattern) to dynamically bind a ListView to an xml file?
Actually the ListView is binded to a static path, like:
C:\DocumentsAndSettings\blabla\morebla\log.xml
I need something like
AppPath\log.xml
Here's my code:
in the View.xaml:
<ListView ItemsSource="{Binding Source={StaticResource logDataSource}, ...
in the App.xaml:
<XmlDataProvider x:Key="logDataSource"
Source="C:\DocumentsAndSettings\blabla\morebla\log.xml"
d:IsDataSource="True"/>
I'd like something like this:
<XmlDataProvider x:Key="logDataSource"
Source="AppPath\log.xml"
d:IsDataSource="True"/>
Thank you in advance.

Why don't you just use relative paths? For example, if you put your log.xml in data directory in your project, simply write
<XmlDataProvider x:Key="logDataSource" Source="data/log.xml" />
Note that the file should have a build action of "resource".
Or if it's a "content" then set copy to o/p directory to "copy always".
(Search for difference b/w these two on Google)

If I put the log.xml file in my project directory, the logger library will write in the
project/bin/debug/log.xml
while the xaml binding will look for log.xml in
project/log.xml
I solved the problem setting the property of the log.xml file to "Content" not to "Resource"..even if I really don't know the difference :)

Related

How to set the Edge label in GraphX

I am using GraphX in Winform project. I am trying to display labels besides the edges. I want to know what property do I have to set in order to display some text in the label.
I have tried setting the 'Text' property of DataEdge, and then calling
ShowAllEdgesLabels(true);
but it does not work this way. Going through the forums I have found that WPF has a way to bind this property to the visual control. The XAML code is as follows
<gxl:EdgeLabelControl x:Name="PART_edgeLabel" Content="{Binding Edge.Text, RelativeSource={RelativeSource TemplatedParent}}" />
Now the question is what is the equivalent of Winform to achieve this functionality.
I found a solution with the help of the admin at the host of GraphX (PantheR).
Basically, we need to add the hostControler for WPF in a windows form.
We need to add a custom XAML template in the resources folder.
We need to load the XAML as a new resource in the code, before we initialize the graph.
We need to add a line of code to merge the resources.
Then in the XAML code we do the binding as mentioned in the question. The code has been updated at the repository to reflect these changes.
The downfall of this solution is that, we need to provide a XAML resource file with the program, but thats just another resource (in my opinion).
For anyone that need some reference code from #ResVic's answer:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:graphx="http://schemas.panthernet.ru/graphx/"
xmlns:local="clr-namespace:YOUR_NAME_SPACE">
...
<Style TargetType="{x:Type graphx:AttachableEdgeLabelControl}">
<Setter Property="ShowLabel" Value="False" />
</Style>
...
</ResourceDictionary>
The Show case demo is protentially helpful for figuring out what stuff the lib could do and how to tweak it to work.

How to localize a WP8 class library?

I'm trying to use resource files (.resx) in a class library. I'm having trouble using these resources in my library's XAML files because libraries do not come with an App.xaml file. So I can not do:
<Application.Resources>
<local:LocalizedStrings xmlns:local="clr-namespace:WPLocalization" x:Key="LocalizedStrings" />
</Application.Resources>
How do I go about localizing a self-contained WP8 library/assembly?
I found a way but it's rather a work around.
The solution is not to try to localize your controls from XAML, but instead from your behind code.
For example, you define a Button in XAML as follows:
<Button Name="MyButton" />
And then in your partial class behind you set the content of the button programatically as follows:
MyButton.Content = MyLocalizedStrings.Hello;
Of course, in this example you would have a resource file called "MyLocalizedStrings.resx" in your project with a string named "Hello" in it.
This approach solves the problem. The only down side is that you won't be able to see a preview of the localized XAML in the Visual Studio XAML window.

WPF Binding image source from Project Resources

Ok i have in my project Resources about 5 Images. What i want to do is to Bind an Image.Source from my Project Resources. Im C# code its pretty easy, i just do :
ImageHolder.Source = Propetries.Resources.Image1.png.
How can this be done in XAML?
Something like this :
<Image Source={??????}/>
Thanks in advance.
Visual studio will create Resources folder and put your image file into it when you add image to the resx file.
In order to use this image in binding you will need to change build action from None to Resource. After that you can bind as follows:
<Image Source="Resources/your_image_name.png"/>
You can not bind directly to Propetries.Resources.your_image_name because of you will need to convert System.Drawing.Bitmap to WPF BitmapSource. But you can bind to strings in the Resource.resx:
<TextBlock Text="{x:Static properties:Resources.YourStringResource}"></TextBlock>
Read here how to convert System.Darwing.Bitmap to the WPF bitmap: Load a WPF BitmapImage from a System.Drawing.Bitmap
And here about binding to the values in the resx file: Get values from *.resx files in XAML
Make sure your Build Action for image is marked as Resource and then you can simply do this in your XAML -
<Image Source="Properties/Resources/a.png"/>
Assuming Propetries/Resources is folder structure in your project where your image is present.

XAML globalization using resources in combobox

I am globalizing my WinRT app and I can't use language resources in my comboboxs. I can use it in my TextBlocks using Text property but not using x:string. What am I doing wrong?
TextBlock x:Uid="Priority" Text="Default"></TextBlock>
<ComboBox>
<x:String x:Uid="Color">Default1</x:String>
<x:String x:Uid="Color.Text">Default2</x:String>
</ComboBox>
EDIT
Why can't I populate ComboBox elements with resource strings in XAML code? I know that I can add TextBlock elements inside ComboBox to use dictionaries or, as I am doing now, load them through code but this is not the response to my question.
As far as I know when you use in xaml that is a compile time constant which will not be able to change at runtime and bind to a resource. That is why you probably see Default1 or Default2 as items in your combobox.
I was able to find a solution to your problem.
The idea is that in the combobox you should use ComboBoxItem and set the x:Uid to the value of the resource. But in the resource file, the actual name should be Name.Content, because ComboBoxItem has a ContentPresenter in it as default and not a TextBlock.
So this is the code that worked for me:
In the resource file I have:
Combo.Content ComboBox1
And in the Xaml I used:
<ComboBox>
<ComboBoxItem x:Uid="Combo" />
</ComboBox>
This will populate the ComboBox with an item "ComboBox1" (from the resource file).
I have not tested this to see if it works with resource files for different languages, but I see no reason why it should not work.
I would suspect (but haven't found specific statement in documentation), that the entries in resource.resw would need to be (dependency?) properties of the objects for the automatic resource binding to work. Strings don't have such properties; Default1 isn't a value for String.Content, for example.
Path of least resistance would be using TextBlock for you ComboBox elements. Alternatively, you could load the resource in code and assign it to the string that way.
Is there anything preventing you from using a binding for the ComboBox's ItemsSource? That way you could use a custom object that contains the information you need, as well as a DisplayName property, which can then get the correct resource string from the .resx file.
If that's not an option at all, I'm not sure right now what the solution would be to do the whole thing entirely in XAML.

How do you set the conent of a Silverlight element to a view?

I am updating some xaml I have written to instead use the code behind due to an issue cropping up.
Pane refers to a Telerik RadPane object.
What I need to do semantically is :
pane.Content = PaneView.xaml;
PaneView being a xaml file containing multiple elements and info. This will not work.
I had this working as follows in the previous xaml file, so it is possible to do; though I don't know how. Can anyone help ?
<UserControl x:vws="MyProject.ViewFolder.Views">
...
<telerik:RadPane Header="PaneView" CanUserClose="False" CanFloat="False"
telerik:RadDocking.SerializationTag="PaneView">
<vws:PaneView />
</telerik:RadPane>
What wrapper do I need to put my xaml file in to force this to work?
Thanks very much
If your RadPane control (the XAML) is defined as x:Class="SomeNameSpaceHere.PaneView" you can set the content pretty simply via:
pane.Content = new PaneView();
You can use a simple UserControl as a container:
<UserControl x:Class="SomeNameSpaceHere.PaneView">
...
</UserControl>

Categories

Resources