I am new to WPF programming. Please help me here.
I want the data entered in textboxes to be updated in the xml file. Here is my code.
xaml:
<Window.Resources>
<XmlDataProvider x:Key="credential_data" Source="Credentials.xml" XPath="/Credentials/MB"/>
</Window.Resources>
<StackPanel DataContext="{Binding Source={StaticResource credential_data}}">
<TextBox Height="23" Name="textBox5" Width="188" Text="{Binding XPath=Hostname, Mode=TwoWay}"/>
<TextBox Height="23" Name="textBox6" Width="188" Text="{Binding XPath=Port, Mode=TwoWay}"/>
</StackPanel>
Here is Credentials.xml :
<?xml version="1.0" encoding="utf-8"?>
<Credentials>
<MB>
<Hostname>10.111.123.234</Hostname>
<Port>8080</Port>
</MB>
</Credentials>
What is happening now is that any updates in the xml file gets reflected in the textboxes but any changes in textbox.text does not update the xml file.
where am I going wrong?? :(
Thanks.
Sorry for not giving you a full example, but this CodeProject already has a tutorial project that does exactly what you want
Related
This is for a WPF application with VS2010.
I have a SQL database in EquipmentTracker.mdf.
I have a table called EquipmentData.
This table contains a Column named Manufacturer.
<Window.Resources>
<my:EquipmentTrackerDataSet x:Key="equipmentTrackerDataSet" />
<CollectionViewSource x:Key="equipmentDataViewSource" Source="{Binding Path=EquipmentData, Source={StaticResource equipmentTrackerDataSet}}" />
</Window.Resources>
<TextBox DataContext="{Binding Source={StaticResource equipmentTrackerDataSet}, Path=EquipmentData/Manufacturer}" />
But when I run the application, the TextBox is still blank(empty).
What am I missing here?
Tried this way too...same result; blank TextBox:
<TextBox DataContext="{Binding Source={StaticResource equipmentTrackerDataSet}}" Text="{Binding Path=EquipmentData/Manufacturer}" />
I would still be interested in knowing why it would not display the value when bound directly to the table, but when I created a VIEW and bound to it, it appears to work as I had expected.
<TextBox DataContext="{Binding Source={StaticResource equipmentDataViewSource}}" Text="{Binding Path=Manufacturer}" />
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 have some data saved in a xml file.
Those will be displayed in a listbox.
Now, when i change the listbox Selectedindex, i would like to update the other information in the textblock based on the selectedindex.
is there any way to do this in pure xaml?
if not, how would i bind the Textblock to the selecteditem in the listbox ?
EDIT:
How would i navigate through the Data without using the listbox? i mean using a button to move to next item and other button to move backward..!!
any help is really appreciated..
<Window x:Class="WpfSingleInstance.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<StackPanel
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="Cornsilk">
<StackPanel.Resources>
<XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
<x:XData>
<Inventory xmlns="">
<Books>
<Book ISBN="0-7356-0562-9" Stock="in" Number="9">
<Title>XML in Action</Title>
<Summary>XML Web Technology</Summary>
</Book>
<Book ISBN="0-7356-1370-2" Stock="in" Number="8">
<Title>Programming Microsoft Windows With C#</Title>
<Summary>C# Programming using the .NET Framework</Summary>
</Book>
<Book ISBN="0-7356-1288-9" Stock="out" Number="7">
<Title>Inside C#</Title>
<Summary>C# Language Programming</Summary>
</Book>
</Books>
</Inventory>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<TextBlock FontSize="18" FontWeight="Bold" Margin="10"
HorizontalAlignment="Center">XML Data Source Sample</TextBlock>
<ListBox
Width="265" Height="98" x:Name="lbox" Background="Honeydew" IsSynchronizedWithCurrentItem="True">
<ListBox.ItemsSource>
<Binding Source="{StaticResource InventoryData}"
XPath="*[#Stock='out'] | *[#Number>=8 or #Number=3]"/>
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="12" Foreground="Red">
<TextBlock.Text>
<Binding XPath="Title"/>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel DataContext="{StaticResource InventoryData}">
<TextBlock Text="{Binding XPath=Book/Title}"/>
<TextBox Margin="5,31,98,10" x:Name="textBoxMainDetail" Text="{Binding XPath=Book/Summary}" />
</StackPanel>
</StackPanel>
</Grid>
You could bind like this:
<TextBox Text="{Binding ElementName=lbox, Path=SelectedItem[Title].InnerText}" />
SelectedItem is an XmlElement.
EDIT: Here is a bit of sample code how to access the data of the XmlDataProvider in code behind and apply it as DataContent of a TextBox.
Change the TextBox.Text binding like this:
<TextBox x:Name="textBoxMainDetail" Text="{Binding Path=[Title].InnerText}" />
In code behind get the XML data from the XmlDataProvider and set the DataContext of the TextBox:
XmlDataProvider dataProvider = (XmlDataProvider)stackPanel.Resources["InventoryData"];
XmlElement books = (XmlElement)dataProvider.Document.SelectNodes(dataProvider.XPath)[0];
// set DataContext to an item from the child node collection
textBoxMainDetail.DataContext = books.ChildNodes[0];
Note that the StackPanel with the XmlDataProvider in its resource dictionary has now got a name. If this code shall run during application initialization (e.g. in Window constructor), the XmlDataProvider.IsAsynchronous property must be set to false.
You should now be able to change the DataContext to another indexed item of the books collection in a button click handler.
You need to set the SelectedValuePath as the 'Title' for your listbox. Then simply bind the your textBlock to the selectedValue of your listbox using elementName like this -
<ListBox Width="265" Height="98" x:Name="lbox" Background="Honeydew"
IsSynchronizedWithCurrentItem="True" SelectedValuePath="Title">
</ListBox>
<StackPanel DataContext="{StaticResource InventoryData}">
<TextBlock Text="{Binding Path=SelectedValue, ElementName=lbox}"/>
<TextBox Margin="5,31,98,10" x:Name="textBoxMainDetail" Text="{Binding XPath=Book/Summary}" />
</StackPanel>
</StackPanel>
I have some xaml that will just copy text from one text box to another:
<StackPanel Orientation="Horizontal">
<TextBox Width="100" Height="30" Text="{Binding ElementName=src1, Path=Text}" />
<TextBox x:Name="src1" Width="100" Height="30" />
</StackPanel>
Nothing special, works fine. A bit dumb but just an example.
However if I put the StackPanel as a resource in the Window and create dynamically from code, like this:
<Window.Resources>
<StackPanel x:Key="MySP" Orientation="Horizontal">
<TextBox Width="100" Height="30" Text="{Binding ElementName=src, Path=Text}"/>
<TextBox x:Name="src" Width="100" Height="30" />
</StackPanel>
</Window.Resources>
.. then the element binding doesnt work anymore.
Why? and how to make it work? Any ideas gratefully received.
The following Xaml should work just fine
<Window ...>
<Window.Resources>
<StackPanel x:Key="MySP" Orientation="Horizontal">
<TextBox Width="100" Height="30" Text="{Binding ElementName=src, Path=Text}"/>
<TextBox x:Name="src" Width="100" Height="30" />
</StackPanel>
</Window.Resources>
<StaticResource ResourceKey="MySP"/>
</Window>
You could also use it from code
StackPanel mySP = TryFindResource("MySP") as StackPanel;
if (mySP != null)
{
this.Content = mySP;
}
However, what is the reason for you to have a StackPanel in the Windows Resoures?
If you want to be able to reuse it several times you would have to set x:Shared="False" on the Resource but then you'll get an exception saying something like Cannot register duplicate Name 'src' in this scope the second time you add it.
As far as I'm concerned you should not put that in <Window.Resources>. Only styles, static, dynamic resources and such...
http://msdn.microsoft.com/en-us/library/ms750613.aspx
<Window>
<Window.Resources>
</Window.Resources>
<StackPanel x:Key="MySP" Orientation="Horizontal">
<TextBox Width="100" Height="30" Text="{Binding ElementName=src, Path=Text}"/>
<TextBox x:Name="src" Width="100" Height="30" />
</StackPanel>
<Window>
Having a similar issue, trying to get relative binding to my source control - In my case, I'm creating a designer and need the element as a static so styles can use it's dimensions for centering calculations on a canvas.
Taking a line from [WPF Xaml Namescopes],
ResourceDictionary does not use XAML names or namescopes ; it uses keys instead, because it is a dictionary implementation.
So, directly using ElementName in a resource Dictionary simply does not work, because no name will bind without a NameScope. Also attempted reproducing your situation with Style setters, but no luck - one cannot set an object's name via a Style.
Now, the convoluted solution I cam up with is to
Create a DependencyProperty in the code-behind of the class
you're declaring this resource in.
replace ElementName=Root with RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type namespace:RootClass}} and bind
to said container directly, allowing you to bind to said
DependencyProperty whilst bypassing the names.
If you need bindings to operate between two elements in the same
StaticResource, bind the source to said DependencyProperty as
OneWayToSource or TwoWay, and the destination as OneWay or TwoWay.
1
I have a set of XML as follows:
...<spotTerms>
<terms xmlns= "">
<term tag="1m" display="1M"/>
<term tag="3m" display="3M"/>
<term tag="6m" display="6M"/>...
I have successfully bound a listbox to this XML using the following:
XmlDataProvider x:Key="Symbols" XPath="/symbols" Source="Config\Symbols.xml"/>
...
<ListBox x:Name="SpotMonths"
Style="{StaticResource SymbolChooserListBox}"
ItemsSource="{Binding Source={StaticResource Symbols}, XPath=spotTerms/terms/term/#display}"
SelectionMode="Multiple"
HorizontalContentAlignment="Stretch" Background="#00000000"/>
My plan is to build strings depending on what items have been selected. To this end, I would like to be able to use one XML attribute for display and another for the actual string concatenation.
e.g. I would like to be able to display "1M" but use "1m" at the back end.
Can anyone help with the syntax for this?
EDIT: Robertos's answer helped me out. In case others have the same problem, this is the final piece of XAML I used:
<ListBox x:Name="SpotMonths"
Style="{StaticResource SymbolChooserListBox}"
ItemsSource="{Binding Source={StaticResource Terms}}"
SelectedValuePath="#tag"
DisplayMemberPath="#display"
SelectionMode="Multiple"
HorizontalContentAlignment="Stretch" Background="#00000000" />
The property you're looking for is ItemsControl.DisplayMemberPath.
Your code might look like this:
<XmlDataProvider x:Key="Terms" XPath="/symbols/spotTerms/terms/term" Source="Config\Symbols.xml"/>
<ListBox x:Name="SpotMonths"
Style="{StaticResource SymbolChooserListBox}"
ItemsSource="{Binding Source={StaticResource Terms}, XPath=#tag}"
SelectionMode="Multiple"
HorizontalContentAlignment="Stretch" Background="#00000000"
DisplayMemberPath="#display" />