I have a UI in a window, which is bound to an XML file. I need to update the UI whenever I manually go and change my XML file.
Following is the WAML file:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="dataSource" Source="Data/Media.xml">
</XmlDataProvider>
</Window.Resources>
<Grid>
<ListBox Name="listBox1" DataContext="{Binding Source={StaticResource dataSource}, XPath=/Media/Book/#Title, Mode=OneWay}" />
</Grid>
</Window>
The following is the XML file:
<?xml version="1.0" encoding="utf-8" ?>
<Media>
<Book Author='John' Title='Fish are my aaaa friends' />
<Book Author='Dave' Title='Fish are my enemies' />
<Book Author='Jane' Title='Fish are my food' />
</Media>
I have a window object in the service layer. To initialise it, I did the following:
winobj.DataContext = node.OuterXml; winobj.Show(); where node is my XML string which I get from the server. If I ask you to update this winobj or reset its datacontext so that the UI is updated, how will you do it?
My UI is bound in the following way.
<Window.Resources>
<XmlDataProvider x:Key="Data" XPath="//WindowUpdate" />
</Window.Resources>
<Label Content="{Binding Source={StaticResource Data},
XPath=Window/Children/Label[#id\=\'lblInterval\']/#text}" ...
You could use a FileSystemWatcher to catch changes to the file, how you change the data is up to you (e.g. expose a bindable property and change that).
Related
I would like to add a specific instance of a class to the resources of a page then use that class as a converter, so in my page constructor I put:
this.Resources.Add("converterASD", new ASDConverter());
then bind to it like this:
<ListBox ItemsSource="{Binding Converter={StaticResource converterASD}}"/>
but I keep getting this exception:
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an
exception.
I'm a bit new to WPF, any advice would be appreciated.
We could use more information from the exception.
Two suggestions:
Make sure that you add the resource before the call to InitializeComponent().
Try switching it to a dynamic resource.
You can declare the Converter you would like to use in the resource section of the page like the following example. (I recommend you declare the converter in XAML instead of the code-behind)
Example:
<UserControl x:Class="Views.ConverterExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300"
d:DesignWidth="300">
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</UserControl.Resources>
<Grid>
<CheckBox x:Name="VisibilityController" IsThreeState="False" />
<ListBox
Visibility="{Binding ElementName=VisibilityController, Path=IsChecked,Converter={StaticResource BoolToVisibilityConverter}}"
Height="100" Width="100" BorderBrush="Red" BorderThickness="1" />
</Grid>
</UserControl>
I have an xml file that looks something like this
<Data>
<NumberID>23423</NumberID>
<NumberID>34234</NumberID>
<NumberID>45435</NumberID>
</Data>
How do I display a specific node on a textbox control? Currently I'm trying to show the second "NumberID" from my XML file:
<Window.Resources>
<XmlDataProvider x:Key="RoutingData"
Source="/RoutingLogic.xml"
XPath="Data/NumberID[2]"/>
</Window.Resources>
...
<TextBox Text="{Binding ElementName=RoutingData}">
But it's now showing anything. What am I doing wrong?
I figured it out. I changed the binding info to this:
<Window.Resources>
<XmlDataProvider x:Key="RoutingData"
Source="/RoutingLogic.xml"
XPath="Data"/>
</Window.Resources>
...
<TextBox Text="{Binding Source={StaticResource RoutingData}, XPath=NumberID[2]}"/>
I'm having some problems with data binding in XAML and WPF. Specifically, I'm trying to bind data from an XmlDataProvider to a ListBox.
The problem is this, when I'm in design mode in Visual Studio 2010, the xml items show up correctly, but when I run the app the listbox is just empty.
Here is what my xaml looks like. I'm not using any code behind, so this is all there is:
<Window x:Class="WpfTest9_Binding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="309" Width="622">
<Window.DataContext>
<XmlDataProvider XPath="Servers">
<x:XData>
<Servers>
<Server name="Server01" active="true" />
<Server name="Server02" active="false" />
<Server name="Testserver01" active="true" />
<Server name="Testserver02" active="true" />
</Servers>
</x:XData>
</XmlDataProvider>
</Window.DataContext>
<Grid>
<ListBox ItemsSource="{Binding XPath=*}" Margin="12">
<ListBox.ItemTemplate>
<DataTemplate>
<Border CornerRadius="5" Margin="5" BorderThickness="2" BorderBrush="#FFC14343">
<StackPanel Orientation="Horizontal" Margin="5">
<CheckBox IsChecked="{Binding XPath=#active}" />
<Label Content="{Binding XPath=#name}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Like I said above, the strange thing is that it looks like it's working while in design mode, but it fails to fill the listbox when I run the application. I'm not getting any error messages or warnings either.
What's wrong?
Ok, the solution was surprisingly simple.
As pointed out in this post, Listbox content not being populated with content from xml when using XmlDataProvider , all I had to do was to add an empty namespace attribute to the xml element. Like this:
<Servers xmlns="">
<Server name="Server01" active="true" />
<!-- ... -->
</Servers>
I'm trying to build a small MVVM test application, but can't really figure how to display my user control in the MainWindow.
My Solution Explorer:
I got a resource dictionary:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MVVM.ViewModel"
xmlns:vw="clr-namespace:MVVM.View">
<DataTemplate DataType="{x:Type vm:ViewModel}">
<vw:View />
</DataTemplate>
</ResourceDictionary>
I got my view:
<UserControl x:Class="MVVM.View.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="PersonTemplate">
<StackPanel>
<TextBlock Text="{Binding FirstName}" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<ListBox ItemsSource="{Binding Path=Persons}"
ItemTemplate="{StaticResource PersonTemplate}" />
</UserControl>
and My MainWindow
<Window x:Class="MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MVVM.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary Source="MainWindowResources.xaml" />
</Window.Resources>
<Grid>
</Grid>
</Window>
The most obvious and easiest way is to add the ContentControl element:
<Grid>
<ContentControl x:Name="mainContentControl" />
</Grid>
And after that set the Content property of this control to your view model, and the corresponding view will be loaded and applied automatically:
this.mainContentControl.Content = new ViewModel.ViewModel();
But I would prefer to use another way without datatemplates:
<Grid>
<vw:View x:Name="mainView"/>
</Grid>
this.mainView.DataContext = new ViewModel.ViewModel();
Build your VS2010 solution, then, go to your MainWindow's XAML.
On the left, there is a toolbar with button "Toolbox"
Open it, it contains all the possible WPF controls you could add to your UI
Your UserControl should appear on top of the list (in a category probably named "MVVM Controls"), just drag&drop it to your UI :)
push
Hello everybody!
I'm using a WPF ComboBox with IsTextSearchEnabled="True" (Autocomplete) and want to bind its ItemsSource-Property to a CompositeCollection. Unfortunately, the Combobox doesn't seem to recognize the items provided by a CollectionContainer within the CompositeCollection. They are shown, but not selected by AutoComplete.
Please try the example, type in "def". If "def" doesn't get selected, you've reproduced the problem I'm facing. Is there any solution, something that I've overseen or a practical way to work around while having some mergedcollection-capability?
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="XData1" XPath="/Info">
<x:XData>
<Info xmlns="">
<Item>def</Item>
<Item>efg</Item>
</Info>
</x:XData>
</XmlDataProvider>
<CollectionViewSource x:Key='Data1' Source="{Binding Source={StaticResource XData1}, XPath=Item}" />
</Window.Resources>
<Grid>
<ComboBox IsEditable="True" IsTextSearchEnabled="True" Margin="0,0,0,283">
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem>abc</ComboBoxItem>
<ComboBoxItem>bcd</ComboBoxItem>
<ComboBoxItem>cde</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource Data1}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
</Grid>
Thanks!
- dartrax
I've found out that this is solved as soon as you override the ToString()-Function of your Items-object, so that it returns what the items DataTemplate shows.
A complete working example is here:
--------> X
dartrax