How to add row in telerik radGridView dynamically? - silverlight

I am using Telerik RadGridView control. I want to add row in radGridView in runtime.
I use following code:
<telerikGrid:RadGridView x:Name="TopGrid" AutoGenerateColumns="False" >
<telerikGrid:RadGridView.Columns>
<telerikGrid:GridViewComboBoxColumn DataMemberBinding="{Binding FieldName}" Width="150" Header="Field Name"/>
<telerikGrid:GridViewComboBoxColumn DataMemberBinding="{Binding Operator}" ItemsSource="{Binding OperatorList, Source={StaticResource OperatorModel}}" Width="150" Header="Operator"/>
</telerikGrid:RadGridView.Columns>
</telerikGrid:RadGridView>
Can anyone help me?

Not sure what you're using as far as a datasource, but generally speaking you'd be binding the RadGridView to some ItemsSource, then the source for the combo box columns would come from there.
For example, with an entity:
public class SomeEntity
{
public string FieldName { get; set; }
public string Operator { get; set; }
}
Binding the RadGridView to the List would automatically generate rows for this, so adding a new entity to that list would generate a new row. More info on how you're binding/populating the grid would help me give a more specific answer. :)

Related

Display the value of a Sub property of a Member in a Collection

I'm binding a ObservableCollection to a Grid, the class T that I am using would have an structure similar to this:
public class MyObject {
public int Id {get;set;}
public string Name {get;set;}
public Address MyAddress {get;set;}
}
public class Address {
public String Street {get;set}
}
So I am binding an ObservableCollection to a Telerik RadGridView and, what I want to do, is to display the property Address.Street for each of the elements in the collection but I cannot just figure out how.
I tried to do bindings like the following:
{Binding MyAddress.Street}
{Binding MyAddress/Street}
{Binding Address.Street}
but non of those work. Is there a way to set the Path property of the binding to do such thing?
Thanks in advance!
If you're using GridViewDataColumn with DataMemberBinding, then you might have to use a cell template to render the sub-member properties (I would have thought DataMemberBinding="{Binding Address.Street}" should work, but if it doesn't, well ...):
<telerik:GridViewDataColumn DataMemberBinding="{Binding Address}">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Street}" />
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>

How to bind Gridview with data at runtime in Windows Phone 7?

In table creation, listbox method doesnt suits to my project need as the table goes beyond phone view. Gridview suited exactly. But I dont know how to add rows at runtime.Could anyone help me please?
namespace myApp
{
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
public Person(string dId, string dName)
{
this.Id = dId;
this.Name = Name;
}
}
}
This is my XAML code:
<Grid x:Name="LayoutRoot" Background="Transparent">
<gridView:GridView x:Name="GridView1" CellSpacing="1" RowSpacing="1" ItemsSource="{Binding}">
<gridView:GridView.Columns>
<gridView:GridViewTextColumn Header="ID" Binding="{Binding} />
<gridView:GridViewTextColumn Header="Name" Binding="{Binding}"/>
</gridView:GridView.Columns>
</gridView:GridView>
</Grid>
To create Statically, i have no problem that I created an viewmodel for person object(e.g personVM) and I created static resource(in XAML) to that object and bound with my gridview.
<phone:PhoneApplicationPage.Resources>
<local:PersonViewModel x:Key="PersonVM"/>
</phone:PhoneApplicationPage.Resources>
<gridView:GridView x:Name="GridView1" CellSpacing="1" RowSpacing="1" ItemsSource="{Binding Source={StaticResource PersonVM}, Path=Data}"
>
How to do this dynamically? (it can be done by *observablecollection??)*I cant do with datacontext.
GridView1.DataContext = new Person("S403","Arthos");
Its not working.How can I identify columns in code.Help me please.
Building a DataGrid Control for Silverlight for Windows Phone
http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-1.aspx
This suits the way to create a dynamic table perfectly, The problem in using list box is if the contents goes beyind windows phone, its not easy then to create table using listbox. Then i cant find any way to find dynamically access the grid view.
This(link) grid control has lot of features that would help a newbie like me. Its totally intuitive.

How do you customize columns in silverlight 4 data binding

This is a very basic question. I have a grid, whose data context is bound to a entity framework service. I simply bound the context to service and I can see the data that is getting bound properly. Now, I want to change couple of coulmns to special controls. Like one column has true or false value and that column I want to display a radio button. One column is date value, I want to display date control. How would one go about doing it?
Thanks.
I'm not exactly sure how to do the Radio Button portion of this, but something like this may get you started:
<ListBox x:Name="LayoutRoot" ItemsSource="{Binding Collection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Text}"/>
<CheckBox Content="True" IsChecked="{Binding Checked, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In this case, you would have a checkbox being bound to the boolean value. I'm not exactly sure what you are using for a date control, but you should be able to place that in the stackpanel also and bind it to the dateproperty of your item.
In the above example, 'Collection' is an observable collection of 'MyObject' which is shown below:
MyObject.cs
public class MyObject
{
public string Text { get; set; }
public bool Checked { get; set; }
public bool InverseChecked { get; set; }
public DateTime Date { get; set; }
}
I also understand that you were using a grid, and I'm showing a ListBox. Not sure if this would work for you, but this is how we've approached it in the past.
Hope this helps!

Silverlight 3 ComboBox ItemTemplate binding

I have a simple ComboBox in my Silverlight 3 application. I want to populate it from an ObservableCollection. The list holds a class that has a Name(string), and a Selected(bool) property. The combo box has as many items as I have in the list, but I cannot seem to get the list data to appear.
Any help would be appreciated.
<ComboBox x:Name="cmbCategory" Grid.Column="3">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<CheckBox IsChecked="{Binding Selected}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
...
private class cmbCategoryClass
{
public string Name { get; set; }
public bool Selected { get; set; }
}
private ObservableCollection<cmbCategoryClass> _categories;
....
cmbCategory.DataContext = _categories;
cmbCategory.ItemsSource = _categories;
I can't tell from your code if this is a Codebehind or a ViewModel. I am guessing that you are actually populating the _categories list in code so that it contains at least one cmbCategoryClass object. Try removing the line that sets the DataContext to _categories as your ItemsSource may be looking for a _categories property on the DataContext Check the Output window in Visual Studio when running in debug mode for clues to data binding failures.

Text Will not show for selected Item in ComboBox

I feel really stupid for asking this but I have been thrashing for over 8 hours. How do I get the Selected Item to show its text in my WPF combo box when selected?
Above is an option dialog that allows users to select and configure the available tournament displays. The problem is the selected combo box item shows the UserControl instead of the Display name.
On Window Loaded:
//_displayer is a private member populated using MEF
//[ImportMany(typeof (IDisplayer))]
//private IEnumerable<IDisplayer> _displayers;
DisplayTypeComboBox.ItemsSource = _displayers;
The ComboBox Xaml:
<ComboBox
Name="DisplayTypeComboBox"
Grid.Column="1"
Grid.ColumnSpan="2"
Grid.Row="1"
IsEditable="False"
SelectionChanged="DisplayTypeComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<ComboBoxItem Content="{Binding DisplayerName}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The IDisplayer:
public interface IDisplayer
{
string DisplayDataLocation { get; set; }
string DisplayerName { get; }
string DisplayerDescription { get;}
bool WatcherEnabled { get; }
UserControl View { get; }
string DisplayerImageLeft { get; set; }
string DisplayerImageRight { get; set; }
void Update();
}
I don't even want to think about how many hours I have spent trying to solve what should be a simple problem. Why is it so hard to get your selected text to appear as the selected value? I give up, WPF you have beat me into submission. I changed the control to a list box it takes up more room to display the selectable Items but at least it works.
<ListBox
Name="DisplayTypeComboBox"
Grid.Column="1"
Grid.ColumnSpan="2"
Grid.Row="1"
SelectionChanged="DisplayType_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding DisplayerName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I encountered the same thing. Took me a while too. :(
You should have used the ItemContainerStyle and not ItemTemplate.
Because ComboBox wraps the internal items with a ComboBoxItem - you basically wrapped the ComboBoxItem with another one.
Check what DisplayerName member actually contains. Most likely it contains the UserControl name instead of the Display name.
Try using a TextBlock to bind to the DisplayerName instead of a ComboboxItem. I believe that when you set the itemsource, the combo control will wrap the items inside comboboxitems controls automatically.
Edit: I misunderstood your question. Try setting the SelectionBoxItemTemplate.

Resources