ComboBox with Checkboxes on selective ComboBox Items - wpf

I have a ComboBox which takes it's values from ObservableCollection which gets it's data from a XML file.
I am able to display the data from XML to the ComboBox whose code is :
<ComboBox x:Name="ComboBox1" ItemSource="{Binding Path = <Property Name>}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedItem, UpdateSourceTrigger=PropertyChanged}">
Now I want to make modifications such that only some Items in the ComboBox have a checkbox which will be based on the text of the ComboBox Item.
If I use Data Template in ComboBox.ItemTemplate, then all the items of combobox will have the checkbox which I don't want.
Is there any way I can achieve this?

In the ItemTemplate, use the DataTemplate in which there will be triggers that change the Visibility of the flag according to the conditions you need.

Related

Set arbitrary value of WPF ComboBox which bound to dictionary

I have the editable ComboBox that using binding to dictionary:
<ComboBox IsEditable="True" ItemsSource="{Binding MyDict}" DisplayMemberPath="Value" SelectedValuePath="Value" SelectedValue="{Binding MyProp}" />
Sometimes I need set in code to property MyProp some different value, that is not presented in the dictionary. In such situation ComboBox is not dispalay the value.
What should I do to ComboBox be able to display arbitrary value in this case?
A ComboBox cannot select arbitrary values. It can only select a value or item that is present in its Items or ItemsSource collection.
So you must add an entry to MyDict before you can select it. You may set the Text property of the ComboBox to a random string, but this won't select any item and set the MyProp property.

listview checkbox binding wpf MVVM

I am trying to get the bool value of the checkbox present in the Listview. I am binding to a bool public property "Assignm" in the view model. I tried the below binding pattern but the problem is if i select one checkbox it selects all checkboxes and vice versa. I think this is because relativesource is listview and it works on complete listview. I also tried changing the relative source to ListviewItem but that didn't trigger anything. Can someone help me please. Do i need to change something here ?
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding MU_Identifier}" IsChecked="{Binding DataContext.Assignm, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}">
</CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
Because your binding for IsChecked property is Assignm property which seems to be one property of your view model.
If there is a boolean property named Assignm for the data model of the DataSource of ListView, then just change the binding like this: {Binding Assignm}, as Tag property does.
All your items are bounded to a single property, so when one item changes a property in your context it changes on other items.
To provide correct work all your items from ItemsSource should have property IsChecked.
Check this Example

Datagrid ComboBox Binding

I have a table in my database that represents a list of areas. I would like to get all of these items in this list and bind them to a combobox dropdown in my datagrid. This currently works, and my grid column displays this dropdown. However, when I select an item in the cell and move to the next row, the cell becomes blank. Also, if the entry in the database has a saved value of "area1", and my dropdown list has "area1,area2,area3,etc", when the grid loads, it doesn't autoselect area1, the cell is simply blank.
In my model:
comboboxColumn1.ItemsSource = ctx.AREAS;//db context loading all areas into combobox
In my xaml:
<DataGrid.Columns>
<DataGridComboBoxColumn DisplayMemberPath="Name" Header="some header" x:Name="comboboxColumn1" SelectedValueBinding="{Binding Name}" />
....
How would I go about setting this selected value so that it is equal to whatever entry is in the db?
You need to set the SelectedValuePath value to your variable property. The DisplayMemberPath is for what is shown, and SelectedValuePath is what is selected. SelectedItem is the actual item that is selected.

wpf combobox binding issue

when i bind one combobox with other combobox items... with the following code
<ComboBox ItemsSource="{Binding ElementName=cbo1, Path=Items}" Name="cbo2" />
it works fine but when i select something from cbo1 and come back to select something in cbo2.. it doesn't list anything nor cbo1 does...
what could be wrong?
The Items property is a CollectionView which wraps the ItemsSource, and includes things like the currently selected item, sort order, etc. If you set ItemsSource on an ItemsControl, your data is automatically wrapped in a CollectionView, and that's what gets set as the Items property. I suspect that this class isn't suitable for sharing between two controls.
If you're using ItemsSource to set the data on cbo1, you could maybe bind to ItemsSource instead? That is:
<ComboBox ItemsSource="{Binding ElementName=cbo1, Path=ItemsSource}" Name="cbo2" />
Haven't had chance to test this, but it's an educated guess :-)

WPF binding to individual 'rows' (not columns) of a datagrid possible?

I have a datagrid. A column of the datagrid is a simple <DataGridTemplateColumn> with its CellTemplate containing a <DataTemplate> which contains a <ComboBox> such as
<my:DataGrid Name="dataGridMain" AutoGenerateColumns="False">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn Header="Food" >
<my:DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<ComboBox Name="comboDataTemplate"
Text="{Binding Path=Food,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Source={StaticResource resFoodLookups}}"
DisplayMemberPath="FoodName"
SelectedValuePath="FoodID" IsEditable="True" />
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
All is working fine. Each combobox is bound to a static list due to the ItemsSource="{Binding Source={StaticResource resFoodLookups}}" statement.
But my requirement is that this list will change from row-to-row.
That is: each time a user types a new entry in the combobox list on one row, I want to have it available in the selection on the next row.
Basically, I want to create a new list for the user each time the user inserts a new word in the combobox on any of the rows. (The combobox is editable).
Now, I can wire up the "ItemsSource=..." at run-time, but I'm only able to do this once thus the <DataTemplate> propagates the 'same' list to 'all' the comboboxes on 'all' the rows.
My thoughts are that I need to change the ItemsSource=... property on an object-by-object basis on each combobox that is created in memory after the DataTemplate has created them - but I have no idea how to do this.
What you need to do is perform 2 way data binding to your the ItemsSource, this way when the ItemSource is updated in one of the combo boxes it will auto update your original collection and therefore your other combo boxes as well.
What I normally do is use the MVVM pattern. It is worth some research if you are not already using a particular pattern on your application.
Using it to solve your problem i would do the following:
Create a ViewModel (Lets call it MyViewModel) which has a collection of values called 'MyComboBoxItems' (It is important that you use ObservableCollection for the databinding to work)
When I create the Window/Control that contains your table, I also create an instance of MyViewModel and set its the Window.DataContext=myViewModelInstance
For your combobox binding use ItemsSource="{Binding Path=MyComboBoxItems, Mode=TwoWay}

Resources