How to bind with XML data using master-detail in WPF XAML? - wpf

I have an XML file with these nodes:
<Product>
<Name>...
<Color>...
<Price>...
</Product>
I have a listbox that displays all the Name's in the XML file like this:
<ListBox Name="listBox1" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel >
<TextBlock Text = "{Binding Name}" />
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When an item in the listbox is selected, I want to display the selected product's Color and Price on 2 label controls.
How do I bind the Color and Price to the selected Name?
This means I need to get the Color and Price info from the XML file because the listbox has only the Name.
Thanks.

The SelectedItem will be the whole item, with all three elements, so something like this should do:
<StackPanel DataContext="{Binding SelectedItem, ElementName=listBox1}">
<TextBlock Text="{Binding XPath=Color}" />
<TextBlock Text="{Binding XPath=Price}" />
</StackPanel>
(Could use Binding.StringFormat to prepend a label, also the Label control itself is for labelling things, not displaying text)

Related

Can't show the DataTemplate on a selected item in combobox?

Newbie question. In the following combobox, the drop-down list correctly displays the company name and phone number as per the DataTemplate. However, selecting an item from the drop-down (by mouse) resutls only in showing the object name:
Stargate_V.DataService.View_Small_Company
The selected company name and phone number is not shown. What am I doing wrong?
<ComboBox
ItemsSource="{Binding PrimaryInsurance.Companies}"
ItemTemplate="{StaticResource CompanyTemplate}"
IsEditable="True"
IsTextSearchEnabled="True"
Height="20" HorizontalAlignment="Left" Margin="375,235,0,0" VerticalAlignment="Top" Width="198" />
The CompanyTemplate is:
<DataTemplate x:Key="CompanyTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Companyname}" Width="240"/>
<TextBlock Text="|" Width="10"/>
<TextBlock Text="{Binding Phone}" Width="80" />
</StackPanel>
</DataTemplate>
TIA
The issue that you are having is the combination of displaying a complex type (i.e. Class) with a DataTemplate and your ComboBox has IsEditable set to true. When setting IsEditable to true the ComboBox doesn't know which property to use for searching so it just calls ToString on the object and displays the results. To fix this issue you need to set the TextSearch.TextPath to the property name on the object that you would like to use for searching. Once you do that it will display that property in the selection box instead of the result of ToString. Below is what your XAML should look like if you wanted to search on the Companyname property on your object.
<ComboBox
ItemsSource="{Binding PrimaryInsurance.Companies}"
ItemTemplate="{StaticResource CompanyTemplate}"
IsEditable="True"
IsTextSearchEnabled="True"
TextSearch.TextPath="Companyname"
Height="20" HorizontalAlignment="Left" Margin="375,235,0,0" VerticalAlignment="Top" Width="198" />

Bind the Text Property of Textblock to a Combobox selectedItem with different DataContext in Silverlight

I have a Data Grid that is bound to a object of type MyStaff. Apart from other properties MyStaff contains a column named LookupID. Now, in my ViewModel I have a collection Named Lookups that have a description for each LookupID.
I have a Template column that has a Textblock in Cell Template and Combobox in CellEdit Template. How do I bind the Textblock so that it dsiplays the description from ComboBox based on LookupID.
I know it would be pretty simple if the datacontext for both the Textblock and ComboBox were simple but that is not the case.
I have tried this but this doesn't work. Any suggestions? Also would appreciate any information on how to best use different Data Context for different controls in Silverlight. For this I have added a static resource pointing to the ViewModel Class.
<sdk:DataGridTemplateColumn Header="Action Point"
Width="500"
CanUserReorder="False"
HeaderStyle="{StaticResource dthFull2}"
IsReadOnly="False">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding ElementName=LookupList,
Path=SelectedItem.Description}"
MinHeight="24"
VerticalAlignment="Top"
Padding="2"
TextTrimming="WordEllipsis"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<my:AutoCompleteComboBox x:Name="LookupList"
FilterMode="Custom" Margin="2,0,0,0"
SelectedValue="{Binding LookupID, Mode=TwoWay}"
SelectedValuePath="LookupID"
ItemsSource="{Binding Path=AnalysisLookupList.Values,
Source={StaticResource ViewModel}}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>

WPF Clearing Validation in BindingGroup

I have a class called Contact that has 2 string properties: "Name" and "PhoneNumber".
My ViewModel has an observable collection of these contacts and there is a ComboBox that has it's ItemsSource bound to this collection .
I have a WPF Grid whose DataContext is set to the ComboBox's SelectedItem property. This Grid has a BindingGroup that contains a ValidationRule to validate the text properties of my "contact" class.
This is my Grid:
<Grid x:Name="ContainerGrid" Grid.Column="0"
Background="Transparent"
LostFocus="ContainerGrid_LostFocus"
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}">
<Grid.BindingGroup>
<BindingGroup x:Name="TextInputBindingGroup" SharesProposedValues="True">
<BindingGroup.ValidationRules>
<local:TextInputValidationRule />
</BindingGroup.ValidationRules>
</BindingGroup>
</Grid.BindingGroup>
<StackPanel>
<TextBlock Text="Name:" />
<TextBox x:Name="PersonName" Text="{Binding Path=PersonName, ValidatesOnExceptions=True}" />
</StackPanel>
<StackPanel>
<TextBlock Text="Phone Number:" />
<TextBox x:Name="PhoneNumber" Text="{Binding Path=PhoneNumber, ValidatesOnExceptions=True}" />
</StackPanel>
</Grid>
If the data for the contact was invalid (the name and/or phone number was missing) the grid containing the elements for editing these properties is highlighted in red and it's tool tip is set to the first error.
If I then delete the invalid Contact, the grid remains outlined in red with the tool tip displaying the error for the item that was deleted even though the grid is now displaying another, valid item.
How do I clear the errors shown in the grid?
Thanks,
-Frinny

How to bind ListBox to a member of a view model in xaml?

I'm just starting with wpf/vmmv. I've seen examples of binding collections to list boxes. Example: in xaml , in code-behind (e.g. Page) "DataContext = collection.. ".
My view model has more properties than just a single collection that need to be bound to a view. Therefore I'd like to set the view model as DataContext for the view and then, in xaml, bind the view model's collection to a ListBox. Assuming that my view model is set as DataContext and it has a property called 'Customers', what is the correct way of binding the property to a ListBox in xaml?
I tried but it does not work.
Thanks.
Do you mean 'how do you bind a collection to a 'ListBox'? You would do that like this:
<ListBox ItemsSource="{Binding Customers}" />
Or this:
<ListBox ItemsSource="{Binding Path=Customers}" />
If you want to bind the internal values of each instance of the Customer class, you would do something like this:
<ListBox ItemsSource="{Binding Customers}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Age}" />
<TextBlock Text="{Binding EyeColour}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I guess you want to display the property "Customers", what you have to do is define ItemTemplate of ListBox, define DataTemplate inside ItemTemplate, and binding Customers to a control, just like below:
<ListBox ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Customers}"/>
......something else you want display
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

wpf binding text to parent listbox's contents

I have a listBox whose items are to be shown in a textbox format, like so:-
<ListBox ItemsSource="{Binding movieList}" Name="innerList">
<ListBox.ItemTemplate >
<DataTemplate >
<TextBox Text="-------" TextChanged="TextBox_TextChanged_1"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
EDIT:
Sorry, movie list was an observablecollection (of Movie) instead of being (of String)
How do I get the textbox to show the contents of its ancestor (the innerList) ?
If you want to display the title of a movie in the TextBox, just use that:
<TextBox Text="{Binding Title}" TextChanged="TextBox_TextChanged_1"/>
(assuming the items in the list are objects with a Title property)
From Binding Declarations Overview
Optionally, a period (.) path can be used to bind to the current
source. For example, Text="{Binding}" is equivalent to Text="{Binding
Path=.}".
So following should do it.
<TextBox Text="{Binding}" TextChanged="TextBox_TextChanged_1"/>

Resources