Bind to the "value" of ObservableCollection<KeyValuePair<object, string>> - c#

I want to bind my combo box ItemsSourse to the "value" i.e. (string component) of the
ObservableCollection<KeyValuePair<object, string>>.
How can I do that?

You could bind the ItemsSource to the ObservableCollection and then set the DisplayMemberPath to Value:
<ComboBox ItemsSource="{Binding YourCollection}" DisplayMemberPath="Value" />
The values in the combo box will then match the Values from the KeyValuePairs.

The easiest way to go would be to use the DisplayMemberPath property:
<ComboBox ItemsSource="{Binding Pairs}" DisplayMemberPath="Value" />
Alternatively, you could expose a new property in your viewmodel that will only contain the values. For example:
public ObservableCollection<string> AllValues { get; set; }
public ViewModel()
{
AllValues = new ObservableCollection<string>(Pairs.Select(x => x.Value));
}
<ComboBox ItemsSource="{Binding AllValues}" />

Related

Wpf ListBox with ComboBox databinding problem Dictionary

In my view is a Listbox with a DataTemplate. The DataTemplate defines 2 columns, one for a TextBlock and one for a ComboBox.
The ListBox:
<ListBox ItemsSource="{Binding SelectedSensorProperty.ValueList}" Margin="5" Grid.Row="0"
ScrollViewer.VerticalScrollBarVisibility="Visible">
The ComboBox is defined as follows:
<ComboBox MinWidth="150"
ItemsSource="{Binding Path=DataContext.SelectedSensorProperty.PossibleValuesList,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
SelectedValuePath="Key"
DisplayMemberPath="Value"
SelectedValue="{Binding Path=Value, Mode=OneWay}"
Grid.Column="1"
Margin="2,0,0,0"/>
My ViewModel contains a variable SelectedSensorProperty of type SensorProperty.
public class SensorProperty
{
public string Name { get; set; }
// key = index value, value = index text
public Dictionary<string, string> PossibleValuesList { get; set; }
// key = feature value, value = index value
public Dictionary<string, string> ValueList { get; set; }
public SensorProperty()
{
ValueList = new Dictionary<string, string>();
}
}
Suppose ValueList contains three KeyValue pairs, then 3 comboxes are shown. The problem is that the selected combobox values are not written to the ValueList. Setting the binding of the combobox to TwoWay gives an error saying the Dictionary is readonly. How to solve this?
You either need to bind the ValuesList ListBox to the selected item of the PossibleValuesList, or catch the SelectionChanged event of the PossibleValuesList and then update the ValuesList dictionary. In both cases though, you need the dictionaries to notify the UI when something changed in them. That can be done by implementing the INotifyPropertyChanged interface, or the dictionaries to be an ObservableCollection instead.

ComboBox binding with DisplayMemberPath and SelectedValuePath does not work as expected

I bind an ObservableCollection to a ComboBox and set the DisplayMemberPath to the Name,
with the help of SelectedValuePath I wanted to achieve that Device.ObjectId gets the value of Object.Id.
But this doesn't seem to work, the Object.Name is still set for Device.ObjectId.
How do I get the ComboBox to show the Name but set the Id?
public class Object
{
public string Id { get; set; }
public string Name { get; set; }
}
ObservableCollection Objects = new ObservableCollection<Object>();
<ComboBox ItemsSource="{Binding Objects}" DisplayMemberPath="Name" SelectedValuePath="Id">
<ComboBox.Text>
<Binding Path="Device.ObjectId" UpdateSourceTrigger="PropertyChanged">
</ComboBox.Text>
</ComboBox>
Since you set the SelectedValuePath attribute to Id, you can bind to the SelectedValue attribute to get the value. Something like this:
<ComboBox ItemsSource="{Binding Objects}" DisplayMemberPath="Name"
SelectedValuePath="Id" SelectedValue="{Binding Device.ObjectId}" />

WPF bind combobox selected value to a string in a view model

I have this combobox in my view:
<ComboBox SelectedValue="{Binding StringObj, UpdateSourceTrigger=PropertyChanged}">
<ComboBoxItem>string0</ComboBoxItem>
<ComboBoxItem>string1</ComboBoxItem>
<ComboBoxItem>string2</ComboBoxItem>
<ComboBoxItem>string3</ComboBoxItem>
</ComboBox>
And in my view model I have this string object:
private string _stringObj;
public string StringObj
{
get { return _stringObj; }
set { _stringObj = value; }
}
How can I bind the selected value from the combobox to the string variable so I can work with it in the view model? This is what I have implemented so far but it doesn't work as I don't understand this binding stuff very well.
You can't set a string property to a ComboBoxItem value.
You could either replace the ComboBoxItems with strings in your XAML and bind the SelectedItem property:
<ComboBox SelectedItem="{Binding StringObj, UpdateSourceTrigger=PropertyChanged}"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<s:String>string0</s:String>
<s:String>string1</s:String>
<s:String>string2</s:String>
<s:String>string3</s:String>
</ComboBox>
Or you could simply set the SelectedValuePath property to "Content":
<ComboBox SelectedValue="{Binding StringObj, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Content">
<ComboBoxItem>string0</ComboBoxItem>
<ComboBoxItem>string1</ComboBoxItem>
<ComboBoxItem>string2</ComboBoxItem>
<ComboBoxItem>string3</ComboBoxItem>
</ComboBox>
This will set your source property to the Content of the selected ComboBoxItem.

Extracting Data from DataGrid into Binded Combobox

What I am trying to do is take the string out of a specific column in my DataGrid and display that string in the Combobox.Text property. I am using this code to do so:
var dataString = ((DataRowView)dgvMain.SelectedItem).Row["Column"].ToString();
I have placed a breakpoint on this to see what it was pulling, and it was the correct String that is contained as an item in the Combobox, but whenever I try to set it via Combobox.Text, the Combobox is empty. I have however set my Combobox to isEditable = True and ReadOnly = True and this method works, but selecting an item in the Collection will display System.Data.DataRowView, due to the Combobox being binded to my DataTable. I'll add the XAML to my Combobox as well:
<ComboBox x:Name="cboAlarmType" HorizontalAlignment="Left" Margin="138,256,0,0" VerticalAlignment="Top" Width="320" TabIndex="5"
BorderBrush="Black" Background="White" ItemsSource="{Binding}" DisplayMemberPath="AlarmName" SelectedValuePath="AlarmName"
SelectedValue="{Binding Row.Column, ElementName=dgvMain}"/>
This method works in Textboxes and Checkboxes, but haven't seemed to figure it out for Comboboxes. Any guidance would be appreciated! :)
It appears to me that your DataContext for the ComboBox must be the DataTable you're using to populate the DataGrid, because ItemsSource="{Binding}". I'm going to assume that it's the DataContext for the DataGrid as well, and that your goal is to have the ComboBox selection reflect the selected row in the DataGrid.
<DataGrid
x:Name="dgvMain"
ItemsSource="{Binding}"
/>
<ComboBox
ItemsSource="{Binding}"
DisplayMemberPath="Column"
SelectedValuePath="Column"
SelectedValue="{Binding SelectedItem.Row[Column], ElementName=dgvMain}"
/>
If you'd like the selection changes to go both ways -- so a change to the ComboBox selection changes the DataGrid selection -- that's easy:
<DataGrid
x:Name="dgvMain"
ItemsSource="{Binding}"
SelectedValuePath="Column"
SelectedValue="{Binding SelectedValue, ElementName=ColumnComboBox}"
/>
<ComboBox
x:Name="ColumnComboBox"
ItemsSource="{Binding}"
DisplayMemberPath="Column"
SelectedValuePath="Column"
SelectedValue="{Binding SelectedItem.Row[Column], ElementName=dgvMain}"
/>
And here's a cleaner way to do the whole thing:
First, write a viewmodel class in C#.
public class ViewModelbase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class MyViewModel : ViewModelbase
{
public MyViewModel()
{
// I populated my DataTable with "Row 1", "Row 2", etc.
// Naturally you'd use a value from your own data.
SelectedColumnValue = "Row 2";
}
#region SelectedColumnValue Property
private String _selectedColumnValue = default(String);
public String SelectedColumnValue
{
get { return _selectedColumnValue; }
set
{
if (value != _selectedColumnValue)
{
_selectedColumnValue = value;
OnPropertyChanged();
}
}
}
#endregion SelectedColumnValue Property
public DataTable Data { get; protected set; }
}
Then use that SelectedColumnValue property to keep track of the selected value of "Column" for both controls. The bindings on the SelectedValue properties of the two controls will be two-way by default, because of framework stuff that makes it be that way (how's that for an explanation?). So if you change SelectedColumnValue programatically, the controls will update, and if the user changes the grid selection, the viewmodel property will be updated, which will in turn update the ComboBox -- and vice versa.
<DataGrid
ItemsSource="{Binding Data}"
SelectedValuePath="Column"
SelectedValue="{Binding SelectedColumnValue}"
/>
<ComboBox
ItemsSource="{Binding Data}"
DisplayMemberPath="Column"
SelectedValuePath="Column"
SelectedValue="{Binding SelectedColumnValue}"
/>
<ComboBox
ItemsSource="{Binding Data}"
DisplayMemberPath="ShoeSize"
SelectedValuePath="Column"
SelectedValue="{Binding SelectedColumnValue}"
/>
Another wrinkle is that DisplayMemberPath and SelectedValuePath needn't be the same. Say we've got a "ShoeSize" column in Data, and we'd like to have those values displayed in another ComboBox. So we can do that. We're still using Column as a unique identifier for rows in Data, but we can tell the ComboBox to display some other column.

WPF Binding Combox with different List and different SelectedValue

In my UserControl ucStep2 I have DataContext of Step2InfoData object that has several properties along with :
private string rockDensUnit;
public string RockDensity_Unit
{
get { return rockDensUnit; }
set
{
if (rockDensUnit != value)
{
rockDensUnit = value;
Changed("RockDensity_Unit");
}
}
}
In my app I got to bind several combo's with different normally measurement types Like {kg/m3, gm/m3}, {meter, cm} and so on such groups of measures. I mean, multiple combo's to have list of same items. So I preferred to create Class's of such lists that I can use in multiple combos. I created ComboItems.cs which contains all items lists that I will need to populate the drop down.
ComboItems.cs
//**OBJECTS I USE FOR LIST OF IEMS**
// Class for kg, gm
public class KgGmItems
{
public ObservableCollection<string> KgGmList { get; set; }
public KgGmItems()
{
KgGmList = new ObservableCollection<string>();
KgGmList.Add("kg/m3");
KgGmList.Add("gram/cm3");
}
public string ValueSelected { get; set; } // Don't know if this is useful in my case
}
// Class for meter, cm
public class MtCmItems : INotifyPropertyChanged
{
public MtCmItems()
{
Dict = new Dictionary<string, string>
{
{"meter", "meter"},
{"centimeter", "centimeter"}
};
}
//...
}
XML i.e. ucStep2 View
<!-- As the objects KgGmItems doesn't contain in ucStep2.xaml.cs or Step2InfoData (that is bound to this UC) so add reference of those classes -->
<UserControl.Resources>
<ObjectDataProvider x:Key="KgGmObj" ObjectType="{x:Type top:KgGmItems}" />
<ObjectDataProvider x:Key="MtCmObj" ObjectType="{x:Type top:MtCmItems}" />
</UserControl.Resources>
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}" SelectedValue="{Binding Path=RockDensity_Unit, Mode=TwoWay}" SelectedIndex="0"
Background="#FFB7B39D" Grid.Row="5" Height="23" HorizontalAlignment="Left" Margin="401,61,0,0" Name="comboBox6" VerticalAlignment="Top" Width="84" Visibility="Hidden">
</ComboBox>
I want to display ObservableCllection KgGmList items from KgGmItems class and bind the selected value to RockDensity_Unit of class Step2InfoData that is bound to this UserControl.
In the above combo, I am able to display all items in the drop down, also 1st item is selected by default. But the value is not bind to RockDensity_Unit; it's value remains null.
I want this to happen 2-way i.e. when RockDensity_Unit proeprtiy's value is set programmatically, the value should be selected in the drop down. Of course the value should exists in the list.
By default the 1st item should be selected.
UPDATE
Added DependencyProperty in ucStep2.xaml.cs
public static readonly DependencyProperty RockDensityUnitProperty =
DependencyProperty.Register("RockDensity_Unit", typeof(string), typeof(UserControl),
new FrameworkPropertyMetadata("kg/m3", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string RockDensity_Unit
{
get { return this.GetValue(RockDensityUnitProperty) as string; }
set { SetValue(RockDensityUnitProperty, value); }
}
XML
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}" SelectedItem="{Binding Path=RockDensity_Unit, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ucStep2}}, Mode=TwoWay}"
Background="#FFB7B39D" Grid.Row="5" Height="23" HorizontalAlignment="Left" Margin="401,61,0,0" Name="comboBox6" VerticalAlignment="Top" Width="84" Visibility="Hidden">
</ComboBox>
ERROR
Error 1 The type reference cannot find a public type named 'ucStep2'. Line 74 Position 194. This refers to the combobox ", "
after FindAncestor
DOUBT
The RockDensity_Unit CLR property in Step2InfoData is untouched.
Why is the code not able to find ucStep2 ? FYI, I think this may be relevant :
<UserControl x:Class="WellBore.ucStep2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WellBore.Models"
xmlns:top="clr-namespace:WellBore"
mc:Ignorable="d"
d:DesignHeight="870" d:DesignWidth="700" MaxHeight="970" MinHeight="700" MaxWidth="600">
Ok, so let's get this binding working... first, I am using an item from your KgGmItems class to bind to the ComboBox. In this class you have a collection of string values to display in the drop down and a string property to bind to the ComboBox.SelectedItem... perfect! Now I'm assuming that you have an instance of this class in the Resources section called KgGmObj... let's keep it simple to start with:
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}"
SelectedItem="{Binding ValueSelected, Mode=TwoWay}" />
This is all you need to setup the binding between the ComboBox and your class. One thing to note though, is that when you try to set the selected item from your code, it will only work if you set it to one of the actual items in the collection... I think that this doesn't really count when using strings, but it's important to know this anyway. If you were setting a custom class as the type of objects in the ComboBox instead, then you could set the selected item like this:
ValueSelected = KgGmList.Where(item => item.Name == "NameOfObjectToMatch").Single();
Or better like this if you had a uniquely identifiable property:
ValueSelected = KgGmList.Where(item => item.Id == Id).Single()
With your string values, you should be able to set the selected item from code like this:
ValueSelected = "Some value";
UPDATE >>> Ok, so let's have another go... I think that I may have enough information to go on now. I think that you want something like this:
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}"
SelectedItem="{Binding RockDensity_Unit, Mode=TwoWay}" />
The problem with this is that you have set the DataContext of the ComboBox to your KgGmObj object. This means that the Framework is going to try to find a property named RockDensity_Unit in that object. I also see another potential problem in your definition of this property.
In order to bind from a UserControl xaml to its code behind, you need to use a DependencyProperty. You can find out how to implement these from the Dependency Properties Overview page at MSDN. So first, I would recommend that you implement your RockDensity_Unit property as a DependencyProperty.
Next, we have to find a way to that property from the ComboBox in the xaml... we can do that using a RelativeSource binding like this:
<ComboBox DataContext="{StaticResource KgGmObj}" ItemsSource="{Binding KgGmList}"
SelectedItem="{Binding RockDensity_Unit, RelativeSource={RelativeSource Mode=
FindAncestor, AncestorType={x:Type ucStep2}}, Mode=TwoWay}" />
Now, if you have a DependencyProperty to bind to the SelectedItem property and your UserControl class is named ucStep2, this should all work... let me know how it goes.
UPDATE 2 >>>
Your error is because you have to add an XML namespace at the top of your XAML file... something like this:
xmlns:YourNamespace="clr-namespace:ApplicationName.FolderNameContainingClass"
Then you use it to reference your class like this:
...AncestorType={x:Type YourNamespace:ucStep2} ...
Also, in your DependencyProperty declaration, you're supposed to supply the name the type of your control, not UserControl, so change
Register("RockDensity_Unit", typeof(string), typeof(UserControl),
to
Register("RockDensity_Unit", typeof(string), typeof(NameOfYourUserControl),
Clearly... replace 'NameOfYourUserControl' with the actual name of your class that extends the UserControl.
Use a Dictionary.
XAML
<ComboBox ItemsSource="{Binding Dict}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedValue="{Binding Prop}"/>
Code Behind
public Dictionary< ValueType, string > Dict { get; private set; }
private ValueType _prop;
public ValueType Prop
{
get{ return _prop }
set
{
_prop = value;
NotifyPropertyChanged( "Prop" ); // Implement INotifyPropertyChanged
}
}
public ViewModel()
{
Dict = new Dictionary< ValueType, string >()
{
{ value1, string1 },
{ value2, string2 },
{ value3, string3 }
};
}

Categories