WPF XML DataBinding not working - wpf

I have below xml file. I have copied it into my project debug/bin folder & have also attached to my project.
<?xml version="1.0" standalone="yes" ?>
- <NorthwindDataSet xmlns="http://tempuri.org/NorthwindDataSet.xsd">
- <Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
- <Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constitución 2222</Address>
<City>México D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customers>
</NorthwindDataSet>
I want to bind properties like CustomerName, City in my WPF application. I tried to bind it in XAML as below, but not getting success. Need suggetsion, what I am doing wrong.
<Window.Resources>
<XmlDataProvider x:Key="NorthData" Source="Northwind.xml" XPath="/Customers"/>
</Window.Resources>
<Grid>
<Label Content="{Binding XPath=Address,FallbackValue=BindingFailed,Source={StaticResource NorthData}}" Height="28" HorizontalAlignment="Left" Margin="118,94,0,0" Name="label1" VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData},XPath=City,FallbackValue=BindingFailed}" Height="100" HorizontalAlignment="Left" Margin="128,144,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>

Using your code I modified the XPath a little bit, and removed the reference to the XSD which you didn't provide, and it worked:
<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="NorthData" XPath="NorthwindDataSet/Customers">
<x:XData>
<NorthwindDataSet xmlns="">
<Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
<Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constitucion 2222</Address>
<City>Mexico D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customers>
</NorthwindDataSet>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<Label Content="{Binding XPath=Address
, FallbackValue=BindingFailed
, Source={StaticResource NorthData}}"
Height="28"
HorizontalAlignment="Left"
Margin="118,94,0,0" Name="label1"
VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData}
, XPath=City
, FallbackValue=BindingFailed}"
Height="100"
HorizontalAlignment="Left"
Margin="128,144,0,0"
Name="listBox1"
VerticalAlignment="Top"
Width="120" />
</Grid>
</Window>

You have to set the DataContext to your XML
<Window.Resources>
<XmlDataProvider x:Key="NorthData" Source="Northwind.xml" XPath="/Customers"/>
</Window.Resources>
<Grid DataContext="{StaticResource NorthData}">
<Label Content="{Binding XPath=Address,FallbackValue=BindingFailed,Source={StaticResource NorthData}}" Height="28" HorizontalAlignment="Left" Margin="118,94,0,0" Name="label1" VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData},XPath=City,FallbackValue=BindingFailed}" Height="100" HorizontalAlignment="Left" Margin="128,144,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>

Related

How to bind a button icon to the selected item of a drop-down list?

In a WPF application and using the Fluent Ribbon Control Suite, I have a DropDownButton that opens up a Gallery that lets the user select a color.
Here is the XAML that creates the button:
<Fluent:DropDownButton x:Name="btnCommentColor" Header="Comments">
<Fluent:DropDownButton.Icon>
<!-- What goes here? -->
</Fluent:DropDownButton.Icon>
<Fluent:Gallery x:Name="galCommentColor" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" SelectedValuePath="Name" MaxItemsInRow="12">
<Fluent:Gallery.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" CornerRadius="2" BorderBrush="Black" Width="25" Height="25" VerticalAlignment="Stretch" Background="{Binding Name}" />
</DataTemplate>
</Fluent:Gallery.ItemTemplate>
</Fluent:Gallery>
</Fluent:DropDownButton>
The SelectedItem of the Gallery returns the Name of the color. I want to make the Icon of the button display the actual color that was selected. Can this be done purely with XAML? I have been trying various things found online but so far have been unable to get anything other than the color name to appear where I want the color rectangle to go. Look for the "What Goes Here?" in the XAML above.
I appreciate any helpful suggestions. Thanks for reading!
UPDATE:
I tried the answer given below and it still doesn't work. I must have something wrong. Here's an updated listing of all the XAML code for this button. Take a look at the XAML for the Gallery itself and the binding for the SolidColorBrush and tell me if you see what i've done wrong.
<Window.Resources>
<ObjectDataProvider MethodName="GetType"
ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp">
<ObjectDataProvider.MethodParameters>
<sys:String>System.Windows.Media.Colors, PresentationCore,
Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}"
MethodName="GetProperties" x:Key="colorPropertiesOdp">
</ObjectDataProvider>
</Window.Resources>
<Fluent:DropDownButton Name="btnCommentColor" Header="Comments">
<Fluent:DropDownButton.LargeIcon>
<Grid Width="32" Height="32">
<Image Source="Icons\BlueLarge.png" />
<Border Height="32" VerticalAlignment="Bottom" BorderThickness="0" CornerRadius="2">
<Border.Background>
<SolidColorBrush Color="{Binding ElementName=galCommentColor, Path=SelectedValue, FallbackValue=Green}" />
</Border.Background>
</Border>
</Grid>
</Fluent:DropDownButton.LargeIcon>
<Fluent:Gallery Name="galCommentColor" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" SelectedValuePath="Name" MaxItemsInRow="12">
<Fluent:Gallery.ItemTemplate>
<DataTemplate>
<Border ToolTip="{Binding Path=Name}" BorderThickness="1" CornerRadius="2" BorderBrush="Black" Width="25" Height="25" VerticalAlignment="Stretch" Background="{Binding Name}" />
</DataTemplate>
</Fluent:Gallery.ItemTemplate>
</Fluent:Gallery>
</Fluent:DropDownButton>
On the page 17 of the walkthrough you have an example of what you are trying to achieve.
You can download it here : http://fluent.codeplex.com/documentation
Taken from the walkthrough :
<fluent1:Ribbon>
<fluent1:Ribbon.Menu>
<fluent1:Backstage />
</fluent1:Ribbon.Menu>
<fluent1:RibbonTabItem Header="Home">
<fluent1:RibbonGroupBox Header="Clipboard">
<!-- The following code shows standard mode for color gallery -->
<fluent1:DropDownButton Header="Standard">
<!-- It's possible to create custom icon to present selected color -->
<fluent1:DropDownButton.Icon>
<Grid Width="16" Height="16">
<Image Source="Images\FontColor.png" />
<Border Height="4"
VerticalAlignment="Bottom"
BorderThickness="0">
<Border.Background>
<SolidColorBrush
Color="{Binding ElementName=ColorGalleryStandard, Path=SelectedColor, FallbackValue=Black}" />
</Border.Background>
</Border>
</Grid>
</fluent1:DropDownButton.Icon>
<fluent1:ColorGallery x:Name="ColorGalleryStandard"
IsNoColorButtonVisible="False"
SelectedColor="Red" />
<fluent1:MenuItem Header="A Menu Item" Icon="Images\Pink.png" />
</fluent1:DropDownButton>
</fluent1:RibbonGroupBox>
</fluent1:RibbonTabItem>
</fluent1:Ribbon>
UPDATE
I see nothing wrong in your code, I've pasted it and it ran successfully, here is it again pasted from my working test.
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fluent1="clr-namespace:Fluent;assembly=Fluent"
xmlns:system="clr-namespace:System;assembly=mscorlib"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<ObjectDataProvider x:Key="colorsTypeOdp"
MethodName="GetType"
ObjectType="{x:Type system:Type}">
<ObjectDataProvider.MethodParameters>
<system:String>
System.Windows.Media.Colors, PresentationCore,
Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
</system:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider x:Key="colorPropertiesOdp"
MethodName="GetProperties"
ObjectInstance="{StaticResource colorsTypeOdp}" />
</Window.Resources>
<fluent1:DropDownButton Name="btnCommentColor" Header="Comments">
<fluent1:DropDownButton.LargeIcon>
<Grid Width="32" Height="32">
<Image Source="Icons\BlueLarge.png" />
<Border Height="32"
VerticalAlignment="Bottom"
BorderThickness="0"
CornerRadius="2">
<Border.Background>
<SolidColorBrush
Color="{Binding ElementName=galCommentColor, Path=SelectedValue, FallbackValue=Green}" />
</Border.Background>
</Border>
</Grid>
</fluent1:DropDownButton.LargeIcon>
<fluent1:Gallery Name="galCommentColor"
ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
MaxItemsInRow="12"
SelectedValuePath="Name">
<fluent1:Gallery.ItemTemplate>
<DataTemplate>
<Border Width="25"
Height="25"
VerticalAlignment="Stretch"
Background="{Binding Name}"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="2"
ToolTip="{Binding Path=Name}" />
</DataTemplate>
</fluent1:Gallery.ItemTemplate>
</fluent1:Gallery>
</fluent1:DropDownButton>
</Window>
Thanks to Aybe for confirming that it's not me. I did get the result I wanted by using a converter on the LargeIcon property of the DropDownButton.
Here's the XAML:
<Fluent:DropDownButton Name="btnCommentColor" Header="Comments" HasTriangle="False" LargeIcon="{Binding ElementName=galCommentColor, Path=SelectedValue, Converter={StaticResource ColorNameToBorderConverter_Key}}">
<Fluent:Gallery x:Name="galCommentColor" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" SelectedValuePath="Name" MaxItemsInRow="12" SelectedIndex="51">
<Fluent:Gallery.ItemTemplate>
<DataTemplate>
<Border ToolTip="{Binding Name}" BorderThickness="1" CornerRadius="2" BorderBrush="Black" Width="25" Height="25" VerticalAlignment="Stretch" Background="{Binding Name}" />
</DataTemplate>
</Fluent:Gallery.ItemTemplate>
</Fluent:Gallery>
</Fluent:DropDownButton>
And the code:
Public Class ColorNameToBorderConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If ApplicationIsInDesignMode Then value = "Black"
If TypeOf value Is String Then
Return New Border With {
.Height = 32,
.BorderThickness = New Thickness(1),
.BorderBrush = New SolidColorBrush(System.Windows.Media.Colors.Black),
.CornerRadius = New CornerRadius(2, 2, 2, 2),
.VerticalAlignment = VerticalAlignment.Bottom,
.Background = New SolidColorBrush(ColorConverter.ConvertFromString(value))
}
Else
Throw New InvalidOperationException("Unsupported type [" & value.GetType.ToString & "]")
End If
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException
End Function
Private Shared ReadOnly Property ApplicationIsInDesignMode() As Boolean
Get
Return CBool(DesignerProperties.IsInDesignModeProperty.GetMetadata(GetType(DependencyObject)).DefaultValue)
End Get
End Property
End Class

how to bind xml file in listbox?

I have an xml file as shown:
<?xml version="1.0" encoding="utf-8"?>
<PhoneBook xmlns="http://tempuri.org/PhoneBook.xsd">
<Person>
<PersonItem name="name1" famil="famil1">
<Numbers>
<NumbersItem number="123456" />
<NumbersItem number="789100" />
</Numbers>
</PersonItem>
</Person>
<Person>
<PersonItem name="name2" famil="famil2">
<Numbers>
<NumbersItem number="654321" />
<NumbersItem number="111213" />
</Numbers>
</PersonItem>
</Person>
</PhoneBook>
I have successfully bound a listbox to this XML using the following:
Imports <xmlns:DaftarTelephone="http://tempuri.org/PhoneBook.xsd">
Dim xmlPhoneBook = XDocument.Load(CurDir() & "\PhoneBook.xml")
Dim q = From el In xmlPhoneBook ...<PhoneBook:PersonItem>
lst.ItemsSource = q 'my listbox
Here's my XAML:
<Page.Resources>
<DataTemplate x:Key="PersonItemTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=Attribute[famil].Value}"/>
<TextBlock Text="{Binding Path=Attribute[name].Value}"/>
<TextBlock Text="{Binding Path=Attribute[number].Value}"/>
</StackPanel>
</DataTemplate>
</Page.Resources>
<Grid x:Name="LayoutRoot" >
<ListBox x:Name="lst" ItemsSource="{Binding}" ItemTemplate="{DynamicResource PersonItemTemplate}" />
</Grid>
family and name is shown correctly. But numbers are not shown!!!

WPF binding root element of xaml file does not work:

I was trying to bind a dataContext to a grid (xaml below)
<Grid .... DataContext="{Binding Path=NewFormViewModel}" > ...</Grid>
This binding did not work and I realized the getter for NewFormViewModel was never being called.
At this point in time, The grid was the root element of the xaml file.
I then placed a canvas inside the Grid and did binding on the canvas like :
<Grid ....>
<Canvas DataContext="{Binding Path=NewFormViewModel}">
....
</Canvas>
</Grid>
The data binding worked.
Next I tried to change the grid to a canvas and do databinding agiain like this:
<Canvas.... DataContext="{Binding Path=NewFormViewModel}" > ...</Canvas>
The binding stopped working again.
In the end I settled for a Grid nested inside a canvas:
<Canvas....>
<Grid DataContext="{Binding Path=NewFormViewModel}">
....
</Grid>
</Canvas>
The question is why did the binding on the root element of the xaml not work?
or Should I have not used a Canvas/Grid as a root element at all and used something like Page/UserControl?
EDIT
My logical tree looks somthing like this:
Window <- Data binding to object o
|
*
Frame <-Data binding to obect o inherited
|
*
Canvas/Grid <- Data binding to o.NewFormViewModel failed
|
*
Canvas/Grid <- Data binding to o.NewFormViewModel Succeeds
EDIT2:
broken xaml:
<Canvas x:Class="WPFEditors.NewForm"
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" Height="398" Width="377"
DataContext="{Binding Path=NewFormViewModel}"
>
<Grid >
<Label DataContext="Form Type" Height="31" HorizontalAlignment="Left" Margin="12,22,0,0" VerticalAlignment="Top" Width="92" />
<ComboBox Margin="148,22,6,347" ItemsSource="{Binding Path=FormTypes}" />
<Label Content="Description" Height="31" HorizontalAlignment="Left" Margin="12,58,0,0" VerticalAlignment="Top" Width="92" />
<Label Content="{Binding Path=Heading}" ToolTip="This is pulled from the Enum defined for FormTypes" Margin="148,59,6,309" />
<Label Content="Version" Height="31" HorizontalAlignment="Left" Margin="12,95,0,0" VerticalAlignment="Top" Width="92" />
<TextBox Text="Bind this later" Margin="148,97,6,270" Height="31" />
<Label Content="Approval Level" Height="31" HorizontalAlignment="Left" Margin="12,132,0,0" VerticalAlignment="Top" Width="92" />
<TextBox Height="31" Margin="148,134,6,233" Text="Bind this later" />
<Label Content="Number of Approvals" Height="31" HorizontalAlignment="Left" Margin="12,171,0,0" VerticalAlignment="Top" Width="130" />
<TextBox Height="31" Margin="148,173,6,194" Text="Bind this later" />
<Label Content="Heading" Height="31" HorizontalAlignment="Left" Margin="12,210,0,0" VerticalAlignment="Top" Width="130" />
<TextBox Height="31" Margin="148,212,6,155" Text="Bind this later" />
<Label Content="Static Data Id" Height="31" HorizontalAlignment="Left" Margin="12,247,0,0" VerticalAlignment="Top" Width="130" />
<TextBox Height="31" Margin="148,249,6,118" Text="Bind this later" />
<Label Content="{Binding Path=Errors}" Background="{Binding Path=Color}" Margin="12,325,6,6" BorderThickness="1" BorderBrush="#FF000019" />
<Button Content="Create" Margin="83,294,202,78" />
<Button Content="Create" Margin="181,294,104,78" />
<Button Content="Create" Margin="279,294,6,78" />
</Grid>
</Canvas>
In the above xaml the following line binds:
<Label Content="{Binding Path=Heading}"
even though Heading is inside a property of the viewmodel that this document inherits.
At this point I would have expected the line:
DataContext="{Binding Path=NewFormViewModel}"
to have changed the datacontext to NewFormViewModel which has no heading. All the remaining bindings fail.
If I change the beginning to :
<Canvas x:Class="NewForm"
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" Height="398" Width="377"
>
<Grid DataContext="{Binding Path=NewFormViewModel}" >
The binding to Heading fails and the rest of the bindings start to work. This is the behaviour that I was expecting initially.
This xaml is nested in this file :
<Window x:Class="WPFEditors.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Path=Heading}" Height="743" Width="1177">
<StackPanel >
<Menu IsMainMenu="True" >
<MenuItem Header="New">
<MenuItem Header="New Form" Command="{Binding Path=MenuCommand}" />
</MenuItem>
<MenuItem Header="Edit Form" ItemsSource="{Binding Path=FormsAvailable}" />
<MenuItem Header="Edit Rules" />
</Menu>
<Frame NavigationUIVisibility="Hidden" Source="{Binding Path=CurrentPage}"
LoadCompleted="Frame_LoadCompleted"
DataContextChanged="Frame_DataContextChanged"
Name="frame">
</Frame>
</StackPanel>
</Window>
and the event handlers to copy the data context to the child are :
private void Frame_LoadCompleted(object sender, NavigationEventArgs e)
{
UpdateFrameDataContext();
}
private void Frame_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
UpdateFrameDataContext();
}
private void UpdateFrameDataContext()
{
var content = frame.Content as FrameworkElement;
if (content == null)
return;
content.DataContext = frame.DataContext;
}
The Binding in the root element should work IF there is something up in the control hierarchy (like a Window or UserControl) that holds (in its DataContext) the object owning the property you're querying (NewFormViewModel).

How to override parent datacontext in child panel

I want to override parent (grid) datacontext in one of the child panel (stackpanel),
such that any update in any of the stackpanel, xml file will get updated accordingly.
Initially I had XPath="/Data/MB" attribute in XmlDataProvider. So with this binding in only textbox 5 and 6 was successful.
Finally what I did is given below. This works fine.
<Grid>
<Grid.DataContext>
<XmlDataProvider x:Name="Credentials" Source="Credentials.xml"/>
</Grid.DataContext>
<StackPanel>
<TextBox Height="23" Name="textBox5" Width="188" Text="{Binding XPath=/Credentials/MessageBroker/Hostname}" />
<TextBox Height="23" Name="textBox6" Width="188" Text="{Binding XPath=/Credentials/MessageBroker/Port}"/>
</StackPanel>
<StackPanel>
<TextBox Height="23" Name="textBox9" Width="188" Text="{Binding XPath=/Credentials/Database/Server}" />
<TextBox Height="23" Name="textBox10" Width="188" Text="{Binding XPath=/Credentials/Database/Password}"/>
</StackPanel>
</Grid>
Here is Credentials.xml
<?xml version="1.0" encoding="utf-8"?>
<Credentials>
<MB>
<Hostname>145.111.227.222</Hostname>
<Port>5672</Port>
<UserName>Admin</UserName>
<Password>Admin</Password>
</MB>
<Database>
<Server>145.111.227.234</Server>
<UserID>Administrator</UserID>
<Password>password</Password>
</Database>
Is there any better way to do it?
Please let me know.

Binding a wpf listbox to a combobox

I have created a very basic wpf application that I want to use to record time entries against different projects.
I havent used mvvm for this as I think its an overkill.
I have a form that contains a combobox and a listbox. I have created a basic entity model like this
What I am trying to do is bind the combobox to Project and whenever I select an item from the combobox it updates the listview with the available tasks associated with that project.
This is my xaml so far. I dont have any code behind as I have simply clicked on that Data menu and then datasources and dragged and dropped the items over. The application loads ok and the combobox is been populated however nothing is displaying in the listbox.
Can anyone tell me what I have missed?
<Window.Resources>
<CollectionViewSource x:Key="tasksViewSource" d:DesignSource="{d:DesignInstance l:Task, CreateList=True}" />
<CollectionViewSource x:Key="projectsViewSource" d:DesignSource="{d:DesignInstance l:Project, CreateList=True}" />
</Window.Resources>
<Grid DataContext="{StaticResource tasksViewSource}">
<l:NotificationAreaIcon
Text="Time Management"
Icon="Resources\NotificationAreaIcon.ico"
MouseDoubleClick="OnNotificationAreaIconDoubleClick">
<l:NotificationAreaIcon.MenuItems>
<forms:MenuItem Text="Open" Click="OnMenuItemOpenClick" DefaultItem="True" />
<forms:MenuItem Text="-" />
<forms:MenuItem Text="Exit" Click="OnMenuItemExitClick" />
</l:NotificationAreaIcon.MenuItems>
</l:NotificationAreaIcon>
<Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="150,223,0,0" Name="btnInsert" VerticalAlignment="Top" Width="46" Click="btnInsert_Click" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="70,16,0,0" Name="comProjects" VerticalAlignment="Top" Width="177" DisplayMemberPath="Project1" ItemsSource="{Binding Source={StaticResource projectsViewSource}}" SelectedValuePath="ProjectID" />
<Label Content="Projects" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" IsEnabled="False" />
<Label Content="Tasks" Height="28" HorizontalAlignment="Left" Margin="16,61,0,0" Name="label2" VerticalAlignment="Top" />
<ListBox Height="112" HorizontalAlignment="Left" Margin="16,87,0,0" Name="lstTasks" VerticalAlignment="Top" Width="231" DisplayMemberPath="Task1" ItemsSource="{Binding Path=ProjectID, Source=comProjects}" SelectedValuePath="TaskID" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="101,224,0,0" Name="txtMinutes" VerticalAlignment="Top" Width="42" />
<Label Content="Mins to Insert" Height="28" HorizontalAlignment="Left" Margin="12,224,0,0" Name="label3" VerticalAlignment="Top" />
<Button Content="None" Height="23" HorizontalAlignment="Left" Margin="203,223,0,0" Name="btnNone" VerticalAlignment="Top" Width="44" />
</Grid>
You set the DataContext in the Grid, so just change your ItemsSource as indicated below.
<Grid DataContext="{StaticResource tasksViewSource}">
<ListBox Height="112"
HorizontalAlignment="Left"
Margin="16,87,0,0"
Name="lstTasks"
VerticalAlignment="Top"
Width="231"
DisplayMemberPath="Task1"
ItemsSource="{Binding}"
SelectedValuePath="TaskID" />
</Grid>
You'll also want to filter the task list, to do that just change the generated code.
Here is an example I hacked together.
You can toggle the value using the SelectionChanged event from your ComboBox.
private System.Data.Objects.ObjectQuery<Models.Task> GetTasksQuery(Models.StackoverflowEntities stackoverflowEntities)
{
// get the selected item
int id = (int)cboProjects.SelectedValue;
System.Data.Objects.ObjectQuery<CollectionViewSourceEF.Models.Task> tasksQuery = stackoverflowEntities.Tasks.Where(task => task.ProjectID == id) as ObjectQuery<Task>;
return tasksQuery;
}

Resources