I'm setting the ItemsSource of a ListBox to the values of a ValueSet instance. Here is ValueSet:
public class ValueSet
{
public string valuetype;
public List<Dictionary<string, object>> values;
public double count;
}
Of course, the ListBox displays each item as "(Collection)" (list box on the right):
Each Dictionary<string,object> element in values is expected to have a pair with a key of text. I'd like for the ListBox to display the value of this pair. Can this be done by setting the DisplayMemberPath of the ListBox? If so what should it be set to? If not, what's a good way to achieve this?
At the very least you should be able to bind using the indexer syntax for binding "[text]". Use the ItemTemplate of the ListBox:
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=[text]}" />
You could also instead try setting DisplayMemberPath="[text]", but I have no idea whether that would work.
Using DisplayMemberPath we can able to achieve this by setting the Binding as Value. Please follow the below code snippet.
DisplayMemberPath="Value"
Related
I have a list of type string(selectedextensionvalue) in my viewmodel. How can I bind selectedvaluepath(which is also of type list string) of my combobox to selectedextensionvalue. I don't know the syntax. May anyone help?
Thanks in advance,
Bilge
If you have a collection of strings, so you do not need to use SelectedValuePath property. You can add some string field to your ViewModel of string type.
public string SelectedStringValue
{
get;
set;
}
and in xaml:
<ComboBox ItemsSource="{Binding Path=selectedextensionvalue}"
SelectedItem="{Binding Path=SelectedStringValue, Mode=OneWayToSource}" />
EDIT:
But, if you want ComboBox to select some special item (for example, which you store in the database), so property SelectedStringValue should to raise PropertyChanged notification in setter, and the xaml will be the following:
<ComboBox ItemsSource="{Binding Path=selectedextensionvalue}"
SelectedItem="{Binding Path=SelectedStringValue, Mode=TwoWay}" />
I am having a ComboBox in wpf which is having its source as a IDictionary<Key, String> where 'Key' is the custom key. The ComboBox is defined as follows:
<ComboBox
x:Name="MD_PDIR_COMBO_SOURCE"
Grid.Row="0"
Style="{DynamicResource USButtonComboBoxStyle}"
Margin="14,5"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Path=SourcesComboList}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedValue="{Binding Path=SelectedSource}"
SelectionChanged="MD_PDIR_COMBO_SOURCE_SelectionChanged"
/>
Now the issue I am facing is, although DisplayMemberPath is set to the "Value" of the dictionary which is a String, the Visual text being displayed in the combo box on selecting an item is BLANK/EMPTY.
Although all the desired functions on combo box selection change are happening correctly but the values/text is not shown after we select an item.
Kindly help!
Does your style set the ItemTemplate at all?
Setting DisplayMemberPath is a shortcut way of saying the ItemTemplate should be a TextBlock with it's Text bound to whatever is in DisplayMemberPath, so setting the ItemTemplate in addition to DispalyMemberPath will override it and make DisplayMemberPath useless
This work OK
public string Value{ get; set; }
This doesen't work !
public string Value;
Conclusion: DisplayMemberPath indicates a property of your item object not a field. Use getter and setter: { get; set; }
i assume that you have a dictionary like this?
SourcesComboList = Dictionary<string,string>();
if you just wanna check wether your Displaymemberpath works alter your combobox to this
<ComboBox Grid.Row="0"
ItemsSource="{Binding Path=SourcesComboList}"
DisplayMemberPath="Value"
SelectedValuePath="Key">
</ComboBox>
if this is working the problem is not DisplayMemberPath :)
I had the similar problem with internal property.
The DisplayMemberPath is working only for public properties.
I guess the issue with the original question might be ComboBox does not query the Dictionary to know its Item Type instead it looks at the individual Dictionary Item (not the Value) which does not have any Key and Value properties.
I have a collection of objects that derive from a Person class and I want to bind this collection to the ItemsSource of a ListView.
I want to specify a string to display in the ListView Items. This string will be a composite of properties found on the derived classes.
I also want to bind the SelectedItem of the ListView to a property of type Person in my view model.
As far as I see it I need a string converter for my display string but I'm unsure how to bind to the items within the ItemsSource to generate the composite display string.
Can anyone give me a pointer?
Thanks.
You can either overwrite the ToString() method of your derived classes to return your composite display string, or you can create a Converter like you are suggesting and pass it the entire Item. The converter would then check that the item is of a specified type, and if so compose a string of whatever properties you want.
you dont need the StringConverter, you need DataTemplate
using DataTemplate, you can choose how you would like to display you data as an item in your listBox.
If you could consider your derived class a ViewModel then you could just add a property to that class and then display it in the ListView ItemTemplate. Or like Rachel suggested override your ToString Method and then in your display binding simply write "{Binding}" which will force WPF to call the ToString method
e.g.
public class DerivedPerson : Person
{
public string DisplayString
{
get
{
return string.Format("{0} {1}",FirstName,LastName);
}
}
}
And you xaml:
<ListView ItemsSource="{Binding PersonList}" SelectedItem="{Binding SelectedPerson}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text={Binding DisplayString}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I try to implement PrepareContainerForItemOverride method of ItemsControl. It will put items to TextBox. It works nice, but how can I binding an item to the textbox text property? One way mode works nice, but when I want two way mode, I have to know the path.
Here is my code:
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
if (element is TextBox)
{
//((TextBox)element).Text = (string)item;
Binding binding = new Binding("I don't know what should i write here.");
binding.Mode = BindingMode.TwoWay;
((TextBox)element).SetBinding(TextBox.TextProperty, binding);
}
}
Thank you for your help!
If the commented line in the code in your question is what you have before then it indicates that the type of item you are providing is String. Two way binding on a string makes no sense the binding would not know where to assign the new value.
The type of items being displayed would need to be some object that has a property of type String, it would be the name of this proprerty that you pass to the Binding constructor.
That said its not clear why you would even need to sub-class ItemsControl in this way. Why not:-
<ItemsControl ItemSource="{Binding SomeEnumberableOfObjectsThatHaveASomeStringProperty}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Test="{Binding SomeString, Mode=TwoWay}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I've got a ComboBox with an ItemsSource which I've bound to a List(Of String).
What I'd like to do is have the XAML update a String property when the SelectedValue of the ComboBox changes. I've seen a whole bunch of examples for TextBoxes which use
Text="{Binding Path=MyString}"
sort of stuff, but I don't really think that'll be the way to go if, in future, I need to change the ItemsSource to a List(Of ObscureObject)...
Binding to the selected property of a combobox is fairly simple.
XAML :
<ComboBox ItemsSource={Binding Path=MyCollection} SelectedItem={Binding Path=MyItem}/>
CodeBehind :
public List<string> MyCollection {get; set;}
public string MyItem {get; set;}
If you want to insert text into the selected item, you'll need to use INotifyPropertyChanged
as for your scalability issue, its a fairly minor change to update the type of a property to reflect a collection. Otherwise you could try binding to an Object although that would mean you would constantly have to recast the object back to the state you want.
You can use SelectedItem property of ComboBox to achieve this.
<ComboBox ItemsSource="{Binding Path=YouList}"
SelectedItem="{Binding Path=MyString}" />
When you change your list in future you will have to bind the SelectedItem with a property of your objects type.
Have a look at this article for more details -
http://japikse.blogspot.com/2008/10/wpf-combobox-selecteditem-selectedvalue.html