ComboBox binding - Silverlight - silverlight-4.0

I am facing a small problem. I am binding ComboBox with two properties. IDTypeCodes is a collection which have IDTYPDSC and IDTYPCDE properties. Ome more collection is PCodes which has a property IDTYPECDE.
I want to add all IDTYPCDE in ComboBox but for displaying purpose, i am using IDTYPDSC. but SelectedValue will be
IDTYPECDE and this value should be displayed when i try to open the wiindow again.
<ns:GridViewDataColumn>
<ns:GridViewDataColumn.CellTemplate>
<DataTemplate>
<ns:ComboBox Margin="8,0"
x:Name = "cmbIDTypeCode"
SelectedValue="{Binding IDTYPECDE,Mode=TwoWay}"
Loaded="cmbIDTypeCode_Loaded" />
</DataTemplate>
</ns:GridViewDataColumn.CellTemplate>
</ns:GridViewDataColumn>
.cs file ============================
private void cmbIDTypeCode_Loaded(object sender, RoutedEventArgs e)
{
cmbIDTypeCode = (NetSolControls.ComboBox)sender;
cmbIDTypeCode.ItemsSource = IDTypeCodes;
cmbIDTypeCode.DisplayMemberPath = "IDTYPDSC";
cmbIDTypeCode.SelectedValuePath = "IDTYPCDE";
}
=========================================================================================
I also have tried in this but the problem is still there. Now how can i get the reference of cmbIDTypeCode ? because it is located in DataTemplate of Grid.
if (cmbIDTypeCode == null) cmbIDTypeCode = new NetSolControls.ComboBox();
Binding bindSelectedValue = new Binding("IDTYPECDE");
bindSelectedValue.Source = Controller.DataContext.PROPOSALAPPLICANT[index].PROPOSALAPPLICANTIDDETAIL;
Binding bindDisplayMemberPath = new Binding("IDTYPDSC");
bindDisplayMemberPath.Source = IDTypeCodes;
Binding bindSelectedValuePath = new Binding("IDTYPCDE");
bindSelectedValuePath.Source = IDTypeCodes;
bindDisplayMemberPath.Mode = BindingMode.OneWay;
bindSelectedValuePath.Mode = BindingMode.OneWay;
bindSelectedValue.Mode = BindingMode.TwoWay;
cmbIDTypeCode.SetBinding(NetSolControls.ComboBox.DisplayMemberPathProperty, bindSelectedValue);
cmbIDTypeCode.SetBinding(NetSolControls.ComboBox.SelectedValuePathProperty, bindSelectedValuePath);
cmbIDTypeCode.SetBinding(NetSolControls.ComboBox.SelectedValueProperty, bindSelectedValue);
================================================================= Have tired in Another way ===========================
object wantedNode = gvIDDetail.FindName("cmbIDTypeCode");returning null. I also tried in this way but still unsuccessfull.
ItemsSource ="{Binding IDTypeCodes.IDTYPECODEInfo}"
DisplayMemberPath="IDTYPDSC"
SelectedValuePath="IDTYPCDE"
SelectedValue="{Binding PropsoalApplicants.PROPOSALAPPLICANTIDDETAILInfo.IDTYPECDE, Mode=TwoWay}" />
</DataTemplate>
</ns:GridViewDataColumn.CellTemplate>
</ns:GridViewDataColumn>

Related

How to do the equivalent of {Binding Source} in code?

I'm extending a control to be able to reuse it across my current Xamarin project. As part of this control, I need to create a DataTemplate programmatically. I have this part figured out and it works ok.
The DataTemplate has a Label in it. I need to bind the Label's BindingContext property to {Binding Source}. I need to bind the Label's Text property to {Binding Path=Name}.
This works in XAML, but I don't want to have to copy it to a million different places in the code base.
<dxGrid:TemplateColumn FieldName="MyPropertyName"
Caption="MyColumn">
<dxGrid:TemplateColumn.DisplayTemplate>
<DataTemplate>
<Label BindingContext="{Binding Source}"
Text="{Binding Source, Path=MyPropertyName}" />
</DataTemplate>
</dxGrid:TemplateColumn.DisplayTemplate>
My extended control looks like this right now:
public class MyColumn : TemplateColumn
{
public MyColumn()
{
DataTemplate displayTemplate = new DataTemplate(() =>
{
BindingBase textBinding = new Binding(FieldName);
Label label = new Label();
// TODO: Bind BindingContextProperty to {Binding Source}
//label.SetBinding(BindingContextProperty, binding);
label.SetBinding(Label.TextProperty, textBinding);
return new ViewCell
{
View = label
};
});
DisplayTemplate = displayTemplate;
}
}
I'm getting hung up in the binding because I'm not sure how to do the equivalent of {Binding Source} in code. Any help would be appreciated.
#Eugene - Thanks for the response. Unfortunately this does not work and binding to "Source" like that throws a Null Reference Exception. I made another pass at it this morning and got it working this way:
public MyColumn()
{
DataTemplate displayTemplate = new DataTemplate(() =>
{
Grid grid = new Grid();
grid.SetBinding(Grid.BindingContextProperty, "Source");
Label label = new Label();
label.SetBinding(Label.TextProperty,FieldName);
grid.Children.Add(label);
return grid;
});
this.DisplayTemplate = displayTemplate;
}
It's simple, use name of property
label.SetBinding(BindingContextProperty, "Source");

Windows Phone 8.1 Toggling the visibility of a TextBlock in a DataTemplate

I'm building a Windows Phone 8.1 Hub Application. One of the hub section contains a ListView that displays a list of articles. I'd like to add a Textblock to this hubsection which displays a message when the articles failed to download. The XAML Code is below:
<HubSection
x:Uid="ArticlesSection"
Header="ARTICLES"
DataContext="{Binding Articles}"
HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
<DataTemplate>
<Grid>
<ListView
AutomationProperties.AutomationId="ItemListViewSection3"
AutomationProperties.Name="Items In Group"
SelectionMode="None"
IsItemClickEnabled="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource BannerBackgroundArticleTemplate}"
ItemClick="ItemView_ItemClick"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
</ListView>
<TextBlock
x:Name="NoArticlesTextBlock"
HorizontalAlignment="Center"
VerticalAlignment="center"
Style="{StaticResource HeaderTextBlockStyle}"
TextWrapping="WrapWholeWords"
TextAlignment="Center"/>
</Grid>
</DataTemplate>
</HubSection>
The problem I'm having is that I can't access the TextBlock from the C# code. Is there an easier way to do this?
The problem I'm having is that I can't access the TextBlock from the C# code.
Yes, since the TextBlock is defined inside a DataTemplate, the TextBlock won't be available until the DataTemplate has been applied. Thus, the x:Name attribute won't automatically generate a variable reference in the InitializeComponent method in your *.g.i.cs file. (Read up on XAML Namescopes for more information).
If you want to access it from your code-behind, there are two ways:
The first way is the simplest: you can get a reference to the TextBlock in the sender argument of the Loaded event handler for that TextBlock.
<TextBlock Loaded="NoArticlesTextBlock_Loaded" />
Then in your code-behind:
private TextBlock NoArticlesTextBlock;
private void NoArticlesTextBlock_Loaded(object sender, RoutedEventArgs e)
{
NoArticlesTextBlock = (TextBlock)sender;
}
The second way is to traverse the visual tree manually to locate the element with the required name. This is more suitable for dynamic layouts, or when you have a lot of controls you want to reference that doing the previous way would be too messy. You can achieve it like this:
<Page Loaded="Page_Loaded" ... />
Then in your code-behind:
static DependencyObject FindChildByName(DependencyObject from, string name)
{
int count = VisualTreeHelper.GetChildrenCount(from);
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(from, i);
if (child is FrameworkElement && ((FrameworkElement)child).Name == name)
return child;
var result = FindChildByName(child, name);
if (result != null)
return result;
}
return null;
}
private TextBlock NoArticlesTextBlock;
private void Page_Loaded(object sender, RoutedEventArgs e)
{
// Note: No need to start searching from the root (this), we can just start
// from the relevant HubSection or whatever. Make sure your TextBlock has
// x:Name="NoArticlesTextBlock" attribute in the XAML.
NoArticlesTextBlock = (TextBlock)FindChildByName(this, "NoArticlesTextBlock");
}
Jerry Nixon has a good page on his blog about this.

how to apply Font.WeightBold for only one ListBoxitem in ListBox

I am binding listboxitems from an arrayList.This arraylist contains all textbox and combobox text.Now my question is how can i make only one listitem font as bold through code not xaml..because am not adding listbox items from xaml.
any suggestion, idea to implement this..
thanks in advance.
EDIT:
xaml:
<ListBox Name="lstbx" Width="200" HorizontalAlignment="Left" Margin="0,0,0,0" BorderBrush="Transparent" > </ListBox>
code:
private ArrayList LoadListData()
{
ArrayList arrList = new ArrayList();
//txtFullName.FontWeight = FontWeights.Bold; //this didnt work
arrList.Add(txtFullName.Text);
arrList.Add(" ");
}
lstbx.ItemsSource=LoadListData();
I don't know if this is what you want but basically I'm filling the listbox with TextBlocks and setting the text content as I want. Just for demonstration:
private void MakeBold()
{
for (int i = 0; i < 5; i++)
{
TextBlock s = new TextBlock();
s.Text = "Testing" + i;
if (i == 3)
s.FontWeight = FontWeights.Heavy;
lstbx.Items.Add(s);
}
}
I get this result:
I hope this helps.
I am assuming txtFullName is declared programmatically. Even if you set the FontWeight programmatically, you are still adding the text only to your list, and the weight information never gets passed along.
Pass the whole TextBox instead and it should work:
arrList.Add(txtFullName);

Master/Detail DataGrid. How to access Detail DataGrid to set ItemsSource?

In a typical Master/Detail situation...
I have a DataGrid. The ItemsSource of this DataGrid is set in the Completed event of a WCF call - (grdMaster.ItemsSource = e.Result) - where the x:Name of the grid is grdMaster. This is all 100%.
However, when adding a Detail Datagrid inside the master grids DataTemplate and naming it appropriately... my codebehind does not recognise the detail grid. So plainly put, I cannot set the ItemsSource of grdDetail like I do with grdMaster.
Depending on the Master item selected, I need to do a WCF call to get the appropriate Details.
Depending on how you are being notified that an item is being selected for expansion you will need to find the row the user is in:
DataGridRow row = DataGridRow.GetRowContainingElement(...);
and update the row details visibility:
row.DetailsVisibility = Visibility.Visible;
Those details aside, you need to create a style for the details rows -- which is wired to an event you can listen to:
<DataTemplate x:Key="DetailsRowTemplate">
<StackPanel>
<Border BorderBrush="{StaticResource BlackBrush}" BorderThickness="0,2,0,0" Padding="0" >
<data:DataGrid ItemsSource="{Binding DummyResultsView}" AutoGenerateColumns="False"
LoadingRow="DataGrid_LoadingRow"
CanUserResizeColumns="False"
CanUserReorderColumns="False"
HeadersVisibility="None"
IsReadOnly="True">
</data:DataGrid>
</Border>
</StackPanel>
</DataTemplate>
which is set as the RowDetailsTemplate for your grid:
Within the LoadingRow event you can obtain a reference to which data context is involved, and save a reference to the child data grid so that after a WCF call you can set the ItemsSource:
private void DataGrid_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
List<DataGrid> detailElements = e.DetailsElement.GetChildrenByType<System.Windows.Controls.DataGrid>().ToList();
var itemSelected = e.Row.DataContext;
if (detailElements.Count > 0)
{
DataGrid detailsDataGrid = detailElements[0];
// save a reference so the ItemsSource can be set later....
this.DataGrid = detailsDataGrid;
this.Model.InitializeDetailsList(detailsDataGrid, itemSelected);
}
}
Hope that helps,

How to set SelectedItem for a ColumnSeries chart from the ViewModel

I have a ColumnSeries chart where I want to control the selected item from the view model. I do this by binding the the SelectedItem of the chart to an object on the view model.
<chartingToolkit:Chart Grid.Row="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderThickness="0" MinHeight="200" Margin="0" x:Name="ratingsChart" Style="{StaticResource ChartWithoutLegendStyle}">
<chartingToolkit:Chart.Series>
<chartingToolkit:ColumnSeries x:Name="chartRatingColSeries" IsSelectionEnabled="True"
SelectedItem="{Binding SelectedRatingDistribution, Mode=TwoWay}"
ItemsSource="{Binding RatingsList}"
IndependentValueBinding="{Binding RatingName}"
DependentValueBinding="{Binding NumberOfGoodies}">
</chartingToolkit:ColumnSeries>
</chartingToolkit:Chart.Series>
</chartingToolkit:Chart>
There are various elements on the page which will force the chart's data to be reloaded (via a web service). When I need to reload the charts data (from the view model), I want to set the SelectedItem of the chart to the very first data point. This appears to work EXCEPT the chart does not visually show (in red, by default) the selected item. Here is sample code that reloads data after web service call and resets selected item:
private RatingDistribution _selectedRatingDistribution = new RatingDistribution();
public RatingDistribution SelectedRatingDistribution
{
get { return _selectedRatingDistribution; }
set
{
_selectedRatingDistribution = value;
RaisePropertyChanged("SelectedRatingDistribution");
}
}
private ObservableCollection<RatingDistribution> _lstRatings = new ObservableCollection<RatingDistribution>();
public ObservableCollection<RatingDistribution> RatingsList
{
get { return _lstRatings; }
set
{
_lstRatings = value;
RaisePropertyChanged("RatingsList");
}
}
private void GetRatingsDistributionCompleted(object sender, GetRatingsDistributionCompletedEventArgs e)
{
IsBusy = false;
RatingsList.Clear();
foreach (RatingDistribution rd in e.Result)
RatingsList.Add(rd);
SelectedRatingDistribution = RatingsList[0];
}
Setting the SelectedRatingDistribution from the View model will not visually show the selected item on the chart in red. Any ideas??
Update:
So if I click on a column, the chart correctly shows the selected item in red as so:
But If I set the SelectedItem from view model, the column will not be displayed in red (as the selected item)
Changed function below fixes problem..
private void GetRatingsDistributionCompleted(object sender, GetRatingsDistributionCompletedEventArgs e) {
RatingsList.Clear();
foreach (RatingDistribution rd in e.Result)
RatingsList.Add(rd);
App.Current.RootVisual.Dispatcher.BeginInvoke(new Action(delegate() { SelectedRatingDistribution = RatingsList[0]; }));
}