silverlight binding grid column from a comboBox using xaml element Binding - silverlight

I want to add a unbound column to a a xamgrid but I want to get the value from a combox, the combobox is bound to channel object at runtime.
I tried adding the unbound column.
Tried:
<Binding Source="{StaticResource ChannelTemplate}" Path="SelectedItem.Content" />
But it doesn't seem to work
I want to bind the channelcode to the grid.
Grid,class code below.
<UI:BaseControl.Resources>
<DataTemplate x:Key="ChannelTemplate" >
<StackPanel Orientation="Horizontal" >
<TextBlock Margin="2" Text="{Binding Path=ChannelName}"/>
</StackPanel>
</DataTemplate>
</UI:BaseControl.Resources>
<ComboBox Name="CboChannel" Margin="59,6,0,0" Height="22" Width="200" VerticalAlignment="Top" HorizontalAlignment="Left" ItemTemplate="{StaticResource ChannelTemplate}" >
</ComboBox>
<ig:XamGrid Name="grdInventory" ColumnWidth="auto" ....
<ig:XamGrid.Columns>
<ig:UnboundColumn Key="Channel">
<ig:UnboundColumn.ItemTemplate>
<DataTemplate >
<TextBlock >
<TextBlock.Text >
<Binding Source="{StaticResource ChannelTemplate}" Path="SelectedItem.Content" />
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ig:UnboundColumn.ItemTemplate>
</ig:UnboundColumn>
</ig:XamGrid.Columns>
</ig:XamGrid>
Code:
Public Class Channel
Implements IDisposable
<DataMember(IsRequired:=True)>
Public Property ChannelID As Long
<DataMember()>
Public Property ChannelDescription As String
<DataMember()>
Public Property ChannelName As String
<DataMember()>
Public Property ChannelCode As String
End Class
Thanks
J

You could try using a DataContext pattern so that your unbound column and combobox selected item are both bound to the same value. For example (I'm using the standard data grid here):
1) Create a DataContextProxy class based on this article: http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx
2) Create a static resource to the DataContextProxy on the page that contains the grid and combo e.g.
<UserControl.Resources>
<Silverlight:DataContextProxy x:Key="Proxy"></Silverlight:DataContextProxy>
</UserControl.Resources>
3) Bind the SelectedValue property of your combo to an exposed property on your VM e.g.
<ComboBox Height="23"
HorizontalAlignment="Left"
SelectedValue="{Binding TestProperty, Mode=TwoWay}"
SelectedValuePath="Content"
Margin="126,54,0,0" x:Name="comboBox1"
VerticalAlignment="Top" Width="120" DisplayMemberPath="Content">
<ComboBox.Items>
<ComboBoxItem x:Name="x" Content="test2"></ComboBoxItem>
<ComboBoxItem x:Name="x2" Content="test"></ComboBoxItem>
</ComboBox.Items>
</ComboBox>
4) Bind the grid column template to the same VM property e.g.
<sdk:DataGrid AutoGenerateColumns="True"
Height="120"
HorizontalAlignment="Left"
Margin="155,153,0,0"
Name="dataGrid1"
VerticalAlignment="Top" Width="120">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Header="Test">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Mode=OneWay,Source={StaticResource Proxy},Path=DataSource.TestProperty}">
</TextBlock>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
This worked for me when I knocked up a quick test app. The DataGridTemplateColumn stays in sync with the selected combobox item.

Related

Link textbox to combobox - not working

I have a WPF usercontrol with a combobox & textbox. I want the textbox to hold the value of the selected item in the combobox and it works fine if I use SelectedValue in the binding path. However if I try to use the Title column of the combobox (SelectedValue.Title) the value of the textbox is overwritten but nothing is displayed. Can anyone tell me what I am doing wrong? My code sample is below. I am a newbie at this so please be kind :)
<ComboBox x:Name="ComboProject" Grid.Column="4" Grid.Row="0" TabIndex="14"
ItemsSource="{Binding Source={StaticResource Projects}, XPath=./Project}"
SelectedValuePath="#Item"
Tag="Project Number"
TextSearch.TextPath="#Item">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text= "{Binding XPath= #Item}" Width="90" />
<TextBlock Text= "{Binding XPath= #Title}" Width="220" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBox x:Name="loaded" Text="{Binding Path=SelectedValue.Title, NotifyOnSourceUpdated=True, ElementName=ComboProject}" Grid.Row="2" Grid.Column="4" Tag="Project Title" TabIndex="15"/>
You set SelectedValuePath="#Item", so that's what SelectedValue has right now. Try setting it to Title and binding directly to SelectedValue:
<ComboBox x:Name="ComboProject"
ItemsSource="{Binding Source={StaticResource Projects}, XPath=./Project}"
SelectedValuePath="#Title">
<ComboBox.ItemTemplate>
...
</ComboBox.ItemTemplate>
</ComboBox>
<TextBox Text="{Binding SelectedValue, ElementName=ComboProject}" />
I removed some other code for clarity of example.
EDIT :
Ok, if you want to use SelectedValue for other purposes we can bind TextBox to SelectedItem instead. If the Title is an attribute of a selected XML node, then we can access it like this:
<TextBox Text="{Binding SelectedItem.Attributes[Title].Value, Mode=OneWay, ElementName=ComboProject}" />

WPF - Filtering a DataCollection with an autocompletebox

I have a view and ViewModel that are working perfectly. I have recently added an AutocompleteBox (found in the WPF Toolkit) which allows users to quickly look up an item.
My view is as such:
An ItemsControl containing my CollectionViewSource named People. Generating perfectly
An AutocompleteBox where the dropdown shows only the items containing the values the user is typing in the AutocompleteBox. Works well. If I type John, all of the people in my CollectionViewSource named People with the word John in the name appear in the dropdown.
My issue is: how do I filter my ItemsControl when the user selects the item he wishes to see from the Dropdown?
My code so far in XAML to bind the data:
<toolkit:AutoCompleteBox Height="25" Width="400"
Foreground="AliceBlue"
ItemsSource="{Binding People.View}"
ValueMemberPath="Name"
Text="{Binding Name}"
IsTextCompletionEnabled="True"
FilterMode="Contains"
Background="#303030">
<toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<Grid Width="360" HorizontalAlignment="Left">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="300">
<TextBlock Text="{Binding Name}" FontWeight="SemiBold" Foreground="#25A0DA"
FontSize="14" Width="300"/>
<TextBlock Text="{Binding Status}" FontWeight="Normal" Foreground="White"
FontSize="10" Width="300"/>
</StackPanel>
</Grid>
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
</toolkit:AutoCompleteBox>
<ItemsControl x:Name="tStack" Grid.Column="0" Grid.Row="1"
ItemsSource="{Binding People.View}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
BorderThickness="0.5">
</ItemsControl>
The itemsControl is styled and the format of the items inside it are also templated/styled but far too long and unconstructive to post here.
From the Name property setter in your viewmodel to which toolkit:AutoCompleteBox.Text is bound, you will have to filter the ObservableCollection backing your Collectionview i,e ItemsSource of your ItemsControl.
If you have your Collectionsource with you then you can have filter applied to it like below:
ICollectionView _peopleView = CollectionViewSource.GetDefaultView(peoples);
_peopleView .Filter = PeopleFilter
private bool PeopleFilter(object item)
{
People people= item as People;
return people.Name.Contains( _filterString ); //Here filter string will be your Name prperty value
}
From Setter of name property you will hav to call _peopleView .Refresh(); to apply the filter
Thanks

Filter in WPF XPath Expression

I have a ComboBox which lists the contact methods shown below. The ComboBox displays the correct values therefore the ItemSource Binding is working.
What i am trying to archive is that to display the selected contact method on application startup. I tried to bind the selected value to the ComboBox.Text attribute but i can't figure out how to set the filter.
This is my input data:
<Contact ShowsInterest="true">
<Name>Tester</Name>
<Lastname>Test</Lastname>
<ContactMethods>
<ContactMethod Selected="False">Phone</ContactMethod>
<ContactMethod Selected="False">Email</ContactMethod>
<ContactMethod Selected="True">Letter</ContactMethod>
<ContactMethod Selected="False">Mobile</ContactMethod>
</ContactMethods>
</Contact>
This is my ComboBox:
<ComboBox Name="combobox1"
ItemsSource="{Binding XPath=Contact/ContactMethods//*}"
Width="100" Height="25">
<ComboBox.Text>
<Binding XPath="Contact/ContactMethods//*[#Selected='true']"/>
</ComboBox.Text>
</ComboBox>
The XPath Expression should do the following: Display the Element under Contact/ContactMethods/ where selected equals true.
EDIT:
Even setting the Text Property directly won't work.
<ComboBox Name="combobox1"
ItemsSource="{Binding XPath=Contact/ContactMethods//*}"
Width="100" Height="25">
<ComboBox.Text>
Phone
</ComboBox.Text>
</ComboBox>
I guess i have to use the SelectedValue Property:
<ComboBox Name="combobox1"
ItemsSource="{Binding XPath=Contact/ContactMethods//*}"
Width="100" Height="25">
<ComboBox.SelectedValue>
Phone
</ComboBox.SelectedValue>
</ComboBox>
EDIT2:
This is the working solution, thanks to MikroDel
<ComboBox Name="combobox1"
ItemsSource="{Binding XPath=Contact/ContactMethods//*}"
Width="100" Height="25">
<ComboBox.SelectedValue>
<Binding XPath="Contact/ContactMethods/ContactMethod[#Selected='True']"/>
</ComboBox.SelectedValue>
</ComboBox>
This is the correct one :
<Binding XPath="Contact/ContactMethods/ContactMethod[#Selected='True']"/>

Element Data-Binding in Silverlight

I have a template column in a DataGrid:
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<TextBlock Text="{Binding Name,ElementName=rsAllSkills}"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
And in the same xaml file, I have
<riaControls:DomainDataSource QueryName="GetSkillsQuery" AutoLoad="True" x:Name="rsAllSkills">
<riaControls:DomainDataSource.DomainContext>
<domain:XXXX context/>
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
The DataSource has loaded everything successfully for sure, if I put that TextBlock out side of the DataGrid, it works; but inside the DataGrid, it doesn't load even the Name of rsAllSkills....
Could anybody give me a hint, thank you so much.
Have a dummy converter and check the binding.
What I guess is, the DataTemplate inside the CellEditingTemplate would receive the parent's DataContext, ie., DataGrid's DataContext. So, to work around this you can do one thing.
1) Bind the rsAllSkills to the Tag Property of DataGridTemplateColumn.
2) Now, Bind the TextBlock's Text property with the Tag property like,
<sdk:DataGridTemplateColumn Tag="{Binding Name,ElementName=rsAllSkills}">
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
<TextBlock Text="{Binding Tag}"/>
</StackPanel>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>

WPF DataGrid - Problem Customizing a ComboBox

I have a Datagrid with a combo box bound column that works great as follows:
<tk:DataGridComboBoxColumn SelectedValueBinding="{Binding DefaultLocationID}"
SelectedValuePath="LocationID"
DisplayMemberPath="LocationName"
Header="Default Location"
ItemsSource="{Binding Source={StaticResource CustomerLocations}}">
</tk:DataGridComboBoxColumn>
Ultimately I want to customize the dropdown to show 'ID' and 'Name' together so a TemplateColumn seems to be the way to go. Starting small, I can't get the following simple example to work which should replace the standard DataGridComboBoxColumn. The dropdown appears fine, but when I 'select' a dropdown item, it does not accept and goes back to the previous value. I've tried variations on 'Mode=TwoWay' but no luck. Do I need a 'CellEditingTemplate' ?
DefaultLocationID is the foreign-key field being edited, while 'LocationID' is a column in 'CustomerLocations'.
<tk:DataGridTemplateColumn Header="Default Location">
<tk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox VerticalAlignment="Top"
SelectedItem="{Binding LocationID, Mode=TwoWay}"
SelectedValue="{Binding DefaultLocationID, Mode=TwoWay}"
ItemsSource="{Binding Source={StaticResource CustomerLocations}}"
SelectedValuePath="LocationID"
DisplayMemberPath="LocationName" >
</ComboBox>
</DataTemplate>
</tk:DataGridTemplateColumn.CellTemplate>
Thanks!!
Can you post the relevant parts of your CustomerLocations resource? Can you also post the type of class that your grid is binding to?
Try removing the SelectedValuePath, DisplayMemberPath and SelectedValue from the ComboBox.
If you want to display multiple pieces of data in your combobox see the below XAML
<ComboBox ...>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding LocationId}"} />
<TextBlock Text=" - "/>
<TextBlock Text="{Binding LocationName}"} />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

Resources