How to set data source of hosted combobox - winforms

I am using a ToolStripControlHost to popup various other controls such as datagridviews, listviews, etc.. How do you assign a datasource to a combobox that is hosted in this manner. Setting the datasource using dataview, datatable, etc does not work. Does anyone know the secret or is this impossible?

The ToolStripComboBox does not support data binding so you'll need to add the items by hand. Thankfully, the combo box has a name on the form so you can just do this:
toolStripComboBox.Items.AddRange(
new object[]
{
"Value 1",
"Value 2",
etc.
}
I don't know what you named the combo box, but just put that name in place of toolStripComboBox.
Update for ComboBox Property
If you wanted to bind via the ComboBox property then you should be able to do something like this:
var cb = toolStripComboBox.ComboBox;
cb.ValueMember = "some field or property";
cb.DisplayMember = "some field or property";
cb.DataSource = {some IEnumerable<T> or DataView or some other sort of bindable list}

Related

Why do widgets update simultaneously when assigned to the same data source?

I have a very basic question about Windows Forms datasources.
If I assign the same object data source to a combobox and a listbox on the same form I observe interesting UI behavior: when I change the item in a combobox (or listbox) the other control selects the same item.
I have no extra code for this UI behavior so I wonder how it works.
var persons = new List<Person>
{
new Person {Id = 1, Age = 10, Name = "Alex"},
new Person {Id = 2, Age = 12, Name = "Boris"},
};
// ListBox
lbPersons.DisplayMember = "Name";
lbPersons.DataSource = persons;
// ComboBox
cbPersons.DisplayMember = "Name";
cbPersons.DataSource = persons;
Please, explain how a control's selected item is changed synchronously?
I have found some similar problems on the web. I don't fully understand how this works, but I will give my best shot at it:
When you have a bind multiple controls to the same datasource, they use the same bindingcontext. Therefore, switching the selected item on one control will change the the selected item on the other control.
Instead when you bind the datasources, give each one a new BindingContext:
lbPersons.DisplayMember = "Name";
lbPersons.DataSource = persons;
lbPersons.BindingContext = new BindingContext();
cbPersons.DisplayMember = "Name";
cbPersons.DataSource = persons;
cbPersons.BindingContext = new BindingContext();
I found this info in a forum (link below) where they confirm the issue and have a solution. I need to do more reading on this, but more info can be found in the msdn at:
http://bytes.com/topic/c-sharp/answers/850851-multiple-controls-bound-same-data-source
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingcontext.aspx
EDIT as per:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.bindingcontext(v=vs.71).aspx
The BindingContext object of a Control is used to return a single
BindingManagerBase object for all data-bound controls contained by the
Control. The BindingManagerBase object keeps all controls that are
bound to the same data source synchronized. For example, setting the
Position property of the BindingManagerBase specifies the item in the
underlying list that all data-bound controls point to.
Also:
(http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingcontext.bindingcontext(v=vs.71).aspx)
For example, if you have two BindingManagerBase objects (from two
different BindingContext objects), you can set the Position properties
of each BindingManagerBase to different values causing each set of
data-bound controls will display different values from the same data
source.

how to bind autocomplete box to a database table column?

i am currently creating a Gridview using telerik control which displays data from the sql database which i displays through domain datasource used in wcf ria.(ADO.net entity model etc)
i want to add an autocomplete box above my radgrid where i type an name and other matchable entries are also listed.
when i click on the entry then radgrid may display whole row containing that name.
i am using silverlight 4,wcf ria,telerik controls.
please provide a sample coding idea in xaml and xaml.cs.
i tries to access telerik demos but they are not running on my system.
As an example... say you have a list of Customers, of which you want to display their names in your AutoComplete box. Further, your Grid should display all customers, and when a Name is selected in the AutoComplete box, the Selected item of the grid displays.
What you need to do is bind the SelectedItem property of the RadGridView & AutoCompleteBox. What I would do is bind the AutoCompleteBox to a property named SelectedName, like so:
<input:AutoCompleteBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName, Mode=TwoWay}" />
Emphasis on the 'Mode=TwoWay' - this is what will alert your code behind that the UI has changed.
In your code behind, you would create properties like this:
private string selectedName;
public string SelectedName
{
get { return selectedName; }
set
{
if (value != null)
{
var query = (from c in CustomersList
where (c.Name == value)
select c).FirstOrDefault();
SelectedCustomer = (Customer)query;
selectedName = value;
}
}
}
Notice how, when you're setting the SelectedName, you're using LINQ to determine which of the customers were selected. One pitfall here would be if you have multiple names in a list... this code only selects the first. If this is an issue, you probably should rethink your architecture..
Then for your grid, you would bind the SelectedItem like so:
<telerik:RadGridView
....
SelectedItem={Binding SelectedCustomer, Mode=TwoWay"}
....
</telerik:RadGridView>
In your code behind, you'd create this property:
private Customers selectedCustomer;
public Customers SelectedCustomer
{
get { return selectedCustomer; }
set {
selectedCustomer = value;
MyGridView.SelectedItem = selectedCustomer;
}
}
Something like that should get you started.
SS

ListView Items sorting?

I have a ListView which contains four column, In which i am adding items dynamically as :
ListViewItem lvi = new ListViewItem();
lvi.Background = ... color you want ... ;
lvi.Content = new {Server = "test1", .... };
listViewResult.Items.Add(lvi);
Now I want to sort this dynamically generated ListView on a perticular column click.
How can I achieve this?
I found article here which explain the custom sorting.
VirtualizingStackPanel.IsVirtualizing=”True”
First you need to specify the above property to true in your ListView, (this is the default value for ListView in WPF).
Then next, you need to use custom sorter, instead of SortDescriptions as described in my earlier blog. The key is using the CustomSort property of ListCollectionView:
ListCollectionView view = (ListCollectionView)CollectionViewSource.GetDefaultView(myListView.ItemsSource);
Then in your ColumnHeader click event handler, you add something like the following:
view.CustomSort = sorter;
myListView.Items.Refresh();
Where sorter is a custom class you implement the IComparer interface.

Set the text and value of a ComboBoxItem

I'm trying to populate a ComboBox programatically. I am creating ComboBoxItems and would like to set their text (the text that is visible for the end-user) and their value (the object that I will handle in the background after the user has selected it.
However the ComboBoxItem seems to only have one member for these two requirements: the Content variable. At the same time this would not fit my needs as I want to distinguish the text and value properties and want to do this without data binding. Is there some viable solution to achieve this?
My current code looks as follows:
ComboBox comboBox;
ComboBoxItem item = new ComboBoxItem();
item.Content = "First Item";
item.Value = 1; // Does not work, no such member as Value!
comboBox.Items.Add(item);
Guess you can use the Tag property.

In WPF, is it possible to have a combo box control, that looks like a RadioButton?

I have a whole bunch of code that is dependent on the ComboBox type, and I want to be able to use it on a new UI, but the UI that I want to use it for needs to look like a RadioButton for usability reasons. Is it possible to have a ComboBox control look like a set of RadioButtons?
My suggestion would be to use an ItemsControl with a DataTemplate that would render RadioButtons. To the ItemsControl you'd bind the same thing you're binding to the ComboBox.
One caveat is that you need to set the GroupName of the radio buttons to something that would be the same to the group, so they can be mutually exclusive. Otherwise, if you don't do anything, you'll be able to select more than one RadioButton simultaneously.
You could build a new UserControl that has many of the same methods that the ComboBox class does, but adapt it so that it creates multiple radio boxes instead.
Your question is kinda vague though.
IE create an Items collection on your user control, and when something is added, draw a radio box and resize your control, instead of what a combo box does and just adds a string to the list.
Then all you have to do is find and replace all your references to ComboBox with RadioIFiedComboBox.
Heres some comparison:
ComboBox cb = new ComboBox();
cb.Items.Add("blah");
or
RadioIFiedComboBox cb = new RadioIFiedComboBox();
cb.Items.Add("blah");
and
public class RadioIFiedComboBox : UserControl {
public ObservableCollection<object> Items = new ObservableCollection<object>();
public RadioIFiedComboBox() {
Items.CollectionChanged += new NotifyCollectionChangedEventHandler(YourCollectionChanged);
}
private void YourCollectionChanged(){
//do something here to redraw your controls
}
}
The code above is just an example, you'd have to create all the methods you use in the ComboBox class and create similar functionality.

Resources