|
In order to get the attribute, you have to use a slightly different type of syntax. Rather than using XPath=name , you have to use XPath=@name . Now, that will only get you the value of the currently selected Kind item, so how do you actually get that item? Well, the answer is to use something like a ListBox to encapsulate the items.
<ListBox ItemsSource="{Binding Source={MyEmulatorsList, XPath=//Kind}" ItemTemplate="{StaticResource KindItemsTemplate}" /> Note: In this sample, I'm assuming that you have used an XmlDataProvider called MyEmulatorsList to point to this XML. So, where is the actual display of the name item? Well, that's provided by the ItemTemplate, which looks like this:
<DataTemplate x:Key="KindItemsTemplate">
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
|
|
|
|
|
Thanks for your reply!!
I think that I'd better expose the whole code.
XML file:
<Emulators>
<System>
<Gear>Name1</Gear>
<Kind name="att1"/>
<Kind name="att2"/>
<Kind name="att3"/>
</System>
</Emulators>
VB code:
Class MainWindow
Dim data As XmlDataProvider = New XmlDataProvider()
Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
data.Source = New Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\myapplication\Systems.xml")
data.XPath = "Emulators/System"
Grid1.DataContext = data
End Sub
End Class
XAML code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525" Background="#FF4E4E64">
<Grid Name="Grid1">
<Grid.Resources>
<DataTemplate x:Key="KindTemplate">
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
</Grid.Resources>
<TextBox Text="{Binding XPath=Gear, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="197,230,0,0" Name="TextBox3" VerticalAlignment="Top" Width="220" />
<ListBox ItemsSource="{Binding Source=data, XPath=//Kind}" ItemTemplate="{StaticResource KindTemplate}" Margin="261,51,12,149" />
</Grid>
</Window>
It doesn't work, I get a
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//Kind' BindingExpression:Path=; DataItem='String' (HashCode=2037252866); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') data
modified 7-Sep-12 13:08pm.
|
|
|
|
|
You are trying to bind to Emulators in your XPath, but your XML says Base.
|
|
|
|
|
You are trying to bind to Emulators in your XPath, but your XML says Base.
That's a typing mistake in my post (I've corrected it in my post)...but actually the XML file is correct..so the mistake doesn't come from here.
|
|
|
|
|
I think I see what the problem is. In the ListBox XAML, you don't need the Source=data (that's incorrect syntax, and besides, you already set the DataContext from the code-behind, so it's not needed. Take that part out, and it should work:
<ListBox ItemsSource="{Binding XPath=//Kind}" ItemTemplate="{StaticResource KindTemplate}"
Margin="261,51,12,149" />
|
|
|
|
|
Thank you very much!! For the first time I can see those data displayed!!
Just one think... of importance:
ALL the "Kind" attributes are displayed.
I've tried a "IsSynchronizedWithCurrentItem="True"" but it doesn't change anything.
Any idea on how to fix it, please?
The datatemplate for the combobox that displays the system's "Name":
<DataTemplate x:Key="SystemTemplate">
<Label Content="{Binding XPath=Gear}"/>
</DataTemplate>
The datatemplate for the ListBox that display the "Kind" name attributes (related to the selected "System"!!):
<DataTemplate x:Key="KindTemplate">
<TextBlock Text="{Binding XPath=@name}"/>
</DataTemplate>
The WPF controls:
<ComboBox Height="37" HorizontalAlignment="Left" Margin="12,262,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="167"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource SystemTemplate}"
IsSynchronizedWithCurrentItem="True">
</ComboBox>
<ListBox ItemsSource="{Binding XPath=//Kind}" ItemTemplate="{StaticResource KindTemplate}" IsSynchronizedWithCurrentItem="True" Margin="425,35,12,149" />
modified 8-Sep-12 6:48am.
|
|
|
|
|
You can select an item by using the XPath expression. Instead of XPath=//Kind, use XPath=//Kind[0], or XPath=//Kind[1], etc.
|
|
|
|
|
dbaseman,
Thanks again for taking time to help me!
If you consider my XML file
<Emulators>
<System>
<Gear>Name1</Gear>
<Kind name="a1"/>
<Kind name="a2"/>
<Kind name="a3"/>
</System>
<System>
<Gear>Name2</Gear>
<Kind name="b1"/>
<Kind name="b2"/>
<Kind name="b3"/>
</System>
</Emulators>
Using "XPath=//Kind[1]" will return: "a1, b1"
What I would like is "a1, a2, a3" being returned!!
How should I please proceed to achieve this (returning all "Kind" attributes of the currently selected "System"?
Thanks very much,
modified 9-Sep-12 6:20am.
|
|
|
|
|
I'm not really an XPath expert, but my guess would be: //System[1]/Kind
|
|
|
|
|
I 'd like to creatre a gallery control just like the built-in gallery in Windows Phone 7. I put a listbox in the grid to show the thumail image, but it cost many memory. And crashed soon after I loaded more than 80 pics(640*480). Could anyone give me a help?
|
|
|
|
|
Thanks for the reply!
I had added the item items to the listbox using databiding like this:
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="2" Tag="{Binding ThumID}" Tap="Grid_Tap">
<Image x:Name="selTag" Width="32" Height="32" Source ="/Resources/Images/sel.png" Visibility="Collapsed" Tag="{Binding FilePath}" VerticalAlignment="Bottom" HorizontalAlignment="Right" Canvas.ZIndex="5"/>
<Image Width="160" Height="120" x:Name="thumImage" Stretch="Uniform" CacheMode="BitmapCache" Source="{Binding ThumBitMap}" />
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Width="200"orderThickness="5" Opened="contentMenu_Opened" Closed="contentMenu_Closed" IsZoomEnabled="False" Background="#c8b3d9ff" BorderBrush="LightGray">
<toolkit:MenuItem x:Name="cm_Del" Header="Delete" Click="cm_Del_Click" Tag="{Binding}"/>
<toolkit:MenuItem x:Name="cm_Edit" Header="Edit" Click="cm_Edit_Click" Tag="{Binding}"/>
<toolkit:MenuItem x:Name="cm_Share" Header="Share" Click="cm_Share_Click" Tag="{Binding}"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
|
|
|
|
|
Hello
I have a TreeView with a HirarchicalDataTemplate. Within this template I would like to set the background of the TreeViewItem depending on a property behind the template.
Thats my xaml:
<TreeView.Resources>
<con:TelegramGroupStateToColorConverter x:Key="TelegramGroupStateToColor"/>
<HierarchicalDataTemplate
DataType="{x:Type tree:ModelTelegramGroup}"
ItemsSource="{Binding Children}"
>
<TextBlock
Text="{Binding Path=Group.Name}"
Tag="{Binding}"
p:ExContextMenu.Menu="{Binding ContextMenu}"
Background ="{Binding Path=Group.State, Converter={StaticResource TelegramGroupStateToColor}}"
/>
</HierarchicalDataTemplate>
</TreeView.Resources>
In opinion it is not rocket sience but it does not work. I can set a breakpoint within the Converter(TelegramGroupStateToColor) and it will be hit and the correct color will be returned, but the backround remains transperent.
Has anybody an idea what I am doing wrong?
|
|
|
|
|
You haven't said what your converter returns. Make sure it returns a System.Windows.Media.Brush and not just a Color or a System.Drawing.Brush. Otherwise this should work.
Hope this helps
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Thanks a lot for the quick answer. You were right.
I returned a System.Drawing.Brush I changed it now and it is working.
Thanks
|
|
|
|
|
Glad to help
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
It is polite to reward a correct answer with n upvote!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi,
I have found a confusing behavior in ContextMenu when processing bound Commands. I want to bind/define a CommandParameter by ItemContainerStyle:
<ContextMenu>
<MenuItem Command="{Binding MyCommand}" />
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="Item" />
<Setter Property="CommandParameter" Value="ValidParameter" />
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
When the menu opens, the CanExecute is called with a command parameter == null! However it is called with the expected parameter immediately before executing the command. But as I want to have the CanExecute return value to depend on the command parameter (enable or disable the menu item), I need the correct parameter also when opening the menu.
Following is a test application to show the bad behavior (see program output for the passed parameter). I would be happy if somebody could help me. By the way, when I directly bind the parameter in MenuItem, everything works fine, but I don't want this because I want to dynamically build the menu using MVVM later.
Thanks!
XAML:
<Window x:Class="CommandParameterExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<Border Margin="4" Height="50" Background="Green">
<Border.ContextMenu>
<ContextMenu>
<MenuItem Command="{Binding MyCommand}" />
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="Item" />
<Setter Property="CommandParameter" Value="ValidParameter" />
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Border.ContextMenu>
</Border>
</StackPanel>
</Window>
Code-Behind:
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
namespace CommandParameterExample
{
public partial class MainWindow
{
public ICommand MyCommand { get; private set; }
public MainWindow()
{
MyCommand = new CommandModel();
InitializeComponent();
}
}
internal class CommandModel : ICommand
{
public bool CanExecute(object parameter)
{
WriteParameter(parameter, "CanExecute");
return true;
}
public void Execute(object parameter)
{
WriteParameter(parameter, "Execute");
}
private static void WriteParameter(object parameter, string context)
{
Debug.WriteLine("Parameter in {0}: {1}", context, parameter ?? "null");
}
public event EventHandler CanExecuteChanged;
}
}
modified 4-Sep-12 5:07am.
|
|
|
|
|
Hello,
I have a ListBox which is bound to an ObservableCollection, using some (complex) ItemTemplate to display the data. It is guaranteed that there is a fixed number of items within the ItemsSource.
Every time, the ObservableCollection changes (e.g., an item is replaced), a new ItemTemplate is generated. Can I somehow avoid this and instead reuse the existing template object and simply bind this to the new data?
My idea was to use a dynamic resource:
<Page.Resources>
<my:Chart x:Key="chart"/>
</Page.Resources>
...
<DynamicResource ResourceKey="chart"/>
But how can I, in this case, set the Properties of the chart (e.g. the DataContext, or the Grid.Row as the object is embedded within a Grid)?
A second problem is: If I have n elements within the ObservableCollection, I need n chart objects. How can I make sure to use a different chart object for each observable?
Alex
|
|
|
|
|
Hi, guys!
I'm a newbie at WPF. I need to make a treeview that has treeviewnode that has a button and a textblock inside. Tried to find out how to make via ControlTemplate, but still unlucky.
Pls, say how to solve such a problem. I think it should be very common situation.
Thanks in advance!
|
|
|
|
|
|
<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">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="SystemTemplate">
<Label Content="{Binding XPath=Name}"/>
</DataTemplate>
</Grid.Resources>
<Grid.DataContext>
<XmlDataProvider x:Name="SystemData" Source="Systems.xml" XPath="Emulators/System" />
</Grid.DataContext>
<ListBox Height="244" HorizontalAlignment="Left" Margin="12,12,0,0" Name="ListBox1" VerticalAlignment="Top" Width="167"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource SystemTemplate}"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single">
</ListBox>
</Grid>
</Window>
Hello,
I've bind an XML file to a ListBox.
Everything's working right..but the XML file is read as a resource for now...what I would like is being able to read it from the users/appdata/roaming folder! (that's where user's data are saved..so I would like to be able to read from there too!)
So, how should I adapt that 'Source=""' part to reference the users/appdata/roaming folder?
Hope that I'm clear! Thanks for your help!
|
|
|
|
|
Amazing, it would never have occurred to me to bind and xml file directly to the UI, I would automatically have loaded the file into a collection in the viewmodel (or code behind) and then bound the collection to the listbox.
I should also note I never use xml!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
You've never read the Adam Nathan book then. His RSS reader sample directly uses this trick.
|
|
|
|
|
Pete O'Hanlon wrote: You've never read the Adam Nathan book
You are right, I have read only one Silverlight book to get the basics and the rest of the learning was by doing...
The direct binding may have it's uses but I only ever do LOB apps and there is always a database sitting behind the app. I have trouble envisioning an app without a database
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi, I'm using Silverlight. Used Charts to display the values. I have applied Styles e.g FontStyle=Normal; for all reports. But while running the program, i'm getting text in Italic. Could anyone help me to resolve this.?
|
|
|
|