WPF combobox dynamic binding - wpf

I've a combo box with in data grid edititemtemplate and i write some code in combo box loaded event like:
Code:
private void cmbGFld_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = (ComboBox)sender;
cmb.ItemsSource = FieldsList.GetFieldList();
ConditionField cData = condLists[FieldGrid.SelectedIndex];
cmb.SelectedItem = cData.FieldType;
}
XAML Code:
<toolkit:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate x:Name="editTemplate">
<ComboBox Loaded="cmbGFld_Loaded" BorderBrush="Transparent" SelectedItem="{Binding Path=FieldType}" SelectedValuePath="Name" BorderThickness="0" FontSize="13" FontStyle="Italic" FontWeight="Normal" Foreground="DimGray" x:Name="cmbGFld" Template="{StaticResource ComboBoxTemplate2}">
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Green</SolidColorBrush>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Left" Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</toolkit:DataGridTemplateColumn.CellEditingTemplate>
But the problem is when ever i try to edit the combo box it doesn't showing which is already selected, any one help me.
Thanks,
#nag.

Try it without cmb.SelectedItem = cData.FieldType; in cmbGFld_Loaded(). This will overwrite the binding SelectedItem="{Binding Path=FieldType}" in your XAML. Set the selected item in the binded FieldType property instead. I don't know your application but something like:
FieldType = condLists[FieldGrid.SelectedIndex].FieldType;

Related

ListPicker issue in WindowsPhone8

Am using a list picker in a WindowsPhone App,I am not able to load the selected element in the list picker,the selected value shows the first item which was loaded during the page load,But the value we get in selection changed event is the correct one.Plz help me
XAML :
<Grid.Resources>
<DataTemplate x:Name="picker">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Margin="0 0 0 0" FontSize="25" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="PickerFullModeItemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Margin="0 0 0 0" FontSize="18" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</StackPanel>
</DataTemplate>
</Grid.Resources>
<toolkit:ListPicker ItemTemplate="{StaticResource PickerFullModeItemTemplate}" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" x:Name="list_city" Grid.Row="3" Grid.Column="5" Grid.ColumnSpan="2" VerticalAlignment="Top" SelectionChanged="list_city_SelectionChanged" Height="170" Grid.RowSpan="1" Margin="12,12,12,0" />
C# :
private void list_city_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int i = list_city.SelectedIndex;
string val = lst_cities[i]; //list of cities
}
Try this! It might work,
private void list_city_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int i = (sender as ListBox).SelectedIndex;
string val = lst_cities[i];
}
You need to bind the SelectedItem property of the ListBox to a property in your ViewModel using TwoWay databinding. If your property has the correct selected item you want when the ListBox loads then it will have the correct selection.

WPF combobox two way binding with TextBlock

I created the following list
List<cb> combolist = new List<cb>();
combolist.Add(new cb() { name = "Neo",bloodgroup = "O+ve"});
combolist.Add(new cb() { name = "meo", bloodgroup = "O" });
combolist.Add(new cb() { name = "bsv", bloodgroup = "B+ve" });
cboxnames.ItemsSource = combolist;
Now I am creating a combobox that gets data from the above list using Item template
<ComboBox Margin="12,31,421,258" Name="cboxnames" IsEditable="False">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Now I am creating an additional textblock that displays the item that is selected in the combobox
<TextBlock Height="28" HorizontalAlignment="Left" Background="LightGray" Margin="0,138,0,0" Text="{Binding UpdateSourceTrigger=PropertyChanged,ElementName=cboxnames,Mode=TwoWay,Path=SelectedItem.Content}" VerticalAlignment="Top" Width="191" />
The problem is whenever I am selecting an item from combobox, that item is not displayed in the textblock, please help!!!
<TextBlock Text="{Binding UpdateSourceTrigger=PropertyChanged,
ElementName=cboxnames,
Mode=TwoWay,Path=SelectedItem.name}"/>
use this..hope it helps you
Hi Check your TextBlock Binding . It should be SelectedItem.name instead of SelectedItem.Content
Text="{Binding UpdateSourceTrigger=PropertyChanged,ElementName=cboxnames,Mode=TwoWay,Path=**SelectedItem.name**}"
I hope this will help.

How to copy Items from Datagrid to Listbox

I have a listbox, datagrid and a Button. The data grid is populated with data from MS SQL. I want to be able to copy selected item(s) from the datagrid to listbox using the button. The code behind the button is
private void btnAdd_click(object sender, RoutedEventArgs e)
{
lstSelected.Items.Add(iFacilitiesDataGrid.SelectedItem.ToString());
}
//List Box in xaml
<ListBox Grid.Column="2" Grid.Row="1" Grid.RowSpan="7" Height="258" HorizontalAlignment="Left" Margin="0,4,0,0" Name="lstSelected" VerticalAlignment="Top" Width="236" />
For the datagrid
<DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Grid.Column="2" Grid.Row="1" Grid.RowSpan="7" Height="244" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource iLocationICategoriesIFacilitiesViewSource}}" Margin="291,5,0,0" Name="iFacilitiesDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="247">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="facilityNameColumn" Header="Facility Name" Width="150">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FacilityName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="priceColumn" Binding="{Binding Path=Price}" Header="Price" Width="100" />
</DataGrid.Columns>
</DataGrid>
Whenever I try to add item on the listbox, the item passed to the listbox is "HM.IFacility" Where HM is the project name and IFacility the Table Name .
If you want the instance of the object to enter the listbox, you cannot control the tostring method like you do:
lstSelected.Items.Add(iFacilitiesDataGrid.SelectedItem.ToString());
Instead, cast your selecteditem to the type:
lstSelected.Items.Add((HM.IFacility)iFacilitiesDataGrid.SelectedItem);
If you don't want the object to enter the listbox, but just some properties from that object, then you have to override the tostring in your object. If you don't do that, the tostring will give you the type of the object, like you said --> "HM.IFacility"
to override the tostring, put this in your object class:
public override string ToString()
{
return property1 + " " + property2
}
Where property1 and property2 are properties on your object class (like name, age, or id)
You need to create a datatemplate for listbox as well. as you have created for DataGrid. replace your listbox with below one and try again.
<ListBox Grid.Column="2" Grid.Row="1" Grid.RowSpan="7" Height="258" HorizontalAlignment="Left" Margin="0,4,0,0" Name="lstSelected" VerticalAlignment="Top" Width="236" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FacilityName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Condition: The data of DataGrid and ListBox must be same Type.

ListBox with hyperlink -> selection changed

I want to do with xaml bindings such feature:
Listbox contains hyperlinks.
When hyperlink clicked - we go to another frame
But also SelectedItem must changed, and on another frame we show info about selected item.
I want it without subscribing click/selected events. Only declarative
Example of my listbox
<ListBox Grid.Row="1"
x:Name="OrderTypesListBox"
ItemsSource="{Binding OrderTypes, Mode=OneWay}"
SelectedItem="{Binding SelectedCall.OrderType, Mode=TwoWay}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
<HyperlinkButton Style="{StaticResource LinkStyle}" NavigateUri="/WindowPage" TargetName="ContentFrame" Content="WindowPage"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now solve like that
<ListBox Grid.Row="1"
x:Name="OrderTypesListBox"
ItemsSource="{Binding OrderTypes, Mode=OneWay}"
SelectedItem="{Binding SelectedCall.OrderType, Mode=TwoWay}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<HyperlinkButton
TargetName="ContentFrame"
NavigateUri="{Binding OrderTypeNextPage}"
Content="{Binding Name}"
Click="HyperlinkButton_Click"
Tag="{Binding}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
OrderTypesListBox.SelectedItem = (sender as HyperlinkButton).Tag;
}
Don't use a HyperlinkButton. Perform the needed actions when the SelectedItem changes in your ViewModel.
Edit: If you need to respond to all click events even if the item is already selected then you can use a Behavior to do this. Just create a behavior for the TextBlock that navigates on the TextBlock click event.
Edit2: Behaviors are pretty simple to code up and easy to use (and don't break the MVVM paradigm).
public class NavigatingTextBlockBehavior : Behavior<TextBlock>
{
protected override void OnAttached()
{
AssociatedObject.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseDown);
}
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
NavigationService.Navigate(new Uri("/WindowPage"));
}
}

How to find sibling of a control that raised an event in DataGrid WPF

I have a DataGrid whole columns are given below.
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Last Name" MinWidth="160" SortMemberPath="[LAST_NAME]">
<my:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<TextBlock Name="lblLastName" Padding="5"
Text="{Binding [LAST_NAME]}" />
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
<my:DataGridTemplateColumn Header="New Age Group" IsReadOnly="True"
MinWidth="130">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedValue="{Binding AgeGroupId}"
DisplayMemberPath="AgeGroupName" ItemsSource="{Binding}"
Name="ddlNewAgeGroup" Loaded="ddlNewAgeGroup_Loaded"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
<my:DataGridTemplateColumn Header="Update" MinWidth="75" Width="100">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Update" Name="btnUpdate" Click="btnUpdate_Click"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
On btnUpdate_Click event I want to get the value that has been set on ddlNewAgeGroup
But I dont know how to find this Combobox on Button Click Event in WPF. I am using DataTables to Bind grids.
(e.OriginalSource as Button).DataContext should be the object binded to your row, so it should have AgeGroupId property (as seen from ComboBox.SelectedValue), and it's a collection (as seen from ComboBox.ItemsSource). So you may find use AgeGroupName this way:
private void Grid_Click(object sender, RoutedEventArgs e) {
var row = (e.OriginalSource as Button).DataContext as %YourDataType%;
var agegroupname = row.First(item => item.AgeGroupId == row.AgeGroupId).AgeGroupName;
// TODO: do what you need with "agegroupname".
}
PS. Please set ComboBox.SelectedValuePath to something like AgeGroupId: without it, ComboBox.SelectedValue is equal to a whole record, not just Id

Resources