Hello I've got combobox in WPF
<ComboBox Name="mcombo" SelectedValuePath="Key" >
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=dictionary}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
also behind i have
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("val", "valvalval");
What I forget? In combobox, no data!
First of all dictionary should be property not variable.
then You should bind Dictionary to ItemsSource
<ComboBox Name="mcombo" SelectedValuePath="Key" ItemsSource="{Binding Dictionary}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}" /> /Dont bind dictionary here. makes no sense
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
hope this helps..
Thanks
Related
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Key}"></TextBlock>
<ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binging SomeProperty}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
I am inside a Dictionary , I want to bind SelectedItem="{Binging SomeProperty}" SomeProperty is a property of the ViweModel, not inside of the Dictionary. How to do that? How can I bind to properties outside of current binding context.
In case of Binding to the Window.DataContext's property, you may do it the following way with RelativeSource:
<ComboBox ItemsSource="{Binding Value}" SelectedItem="{Binding DataContext.SomeProperty, RelativeSource={RelativeSource AncestorType=Window}}">
Or in case you want to display SelectedItem for example in some TextBox (for example collection of string):
<TextBox Text="{Binding MyCollection/}"/>
<ComboBox ItemsSource="{Binding MyCollection}" IsSynchronizedWithCurrentItem="True">
MyCollection with "/" gets the current item of the ICollectionView used as DefaultView for MyCollection. Read more >>>
I currently have a List< KeyValuePair< string, string>> StatusValue
which i populate with 9 KeyValue pairs
I need help with how am I supposed to print that onto view in this fashion:
Key Value
Key Value
Key Value..
For now I tried something like:
<ListBox Grid.Row="0" Grid.Column="0" Margin="10,30,0,0" x:Name="StatusValue"
ItemsSource="{Binding Path=StatusValue}"
DisplayMemberPath="Key"
SelectedValuePath="Value">
</ListBox>
however that doesn't seem to be working at all.
A Listbox is just a container. You need to define a template for it, in which you specify how to display the items. For instance you can do:
<ListBox Grid.Row="0" Grid.Column="0" Margin="10,30,0,0" x:Name="StatusValue"
ItemsSource="{Binding Path=StatusValue}">
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Key}"/>
<TextBlock Text="{Binding Value}"/>
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have an issue with binding in ComboBox. I have searched in Google and so, but I haven't been able to find the answer.
I have silverlight form with combobox like this:
<ComboBox x:Name="FirmBox"
Grid.Row="23"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="5,5,5,0"
SelectedValuePath="{Binding Path=Value, Mode=TwoWay}"
SelectedItem="{Binding Path=Firm, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Value}"/>
<TextBlock Text="{Binding Path=Key}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
And the ItemsSource is an
ObservableCollection<KeyValue<String, KeyValue<String, String>>>
So I've figured out how to display this in right way, but I don't know how to bind the selected item to my KeyValuePair<String, String>
field. That does not seem obvious to me. So I need to bind the value of selected item to my field and don't know how to do it.
Thank you.
The solution was simple as always:
SelectedValuePath="Value"
SelectedValue="{Binding Path=Firm, Mode=TwoWay}">
I want to show in one combobox content from two database columns. I would like to show "Name Surname" but I don't know how. I'm working in C# (.NET) using MVVM pattern. "Name" and "Surname" are fields from table "tblGuests".
Thanks in advance,
Vladimir
You could create an ItemTemplate for the ComboBox that binds to all the properties you want to show.
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}" Padding="10,0,0,0"/>
<TextBlock Text="{Binding Path=LastName}" Padding="10,0,0,0"/>
</StackPanel>
<DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
You could even create a UserControl that can be re-used and use that:
<PersonView>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}" Padding="10,0,0,0"/>
<TextBlock Text="{Binding Path=LastName}" Padding="10,0,0,0"/>
</StackPanel>
</PersonView>
<ComboBox ItemsSource="{Binding}">
<ComboBox.ItemTemplate>
<DataTemplate>
<PersonView/>
<DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Keep a string list property in Viewmodel,
populate that property in a data loading method, by combining name and surname strings,
bind string property to combo box,
<ComboBox Name="ComboBox1" ItemsSource="{Binding YourStringListProperty}"/>
Another option could be to use MultiBinding something like this
<TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource myNameConverter}"
ConverterParameter="FormatLastFirst">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
This code was taken directly from msdn. Refer to it for more details
I don't know how to bind to an int since it has no properties.
<ListBox Name="AgencyTypeListBox" >
<ListBox.ItemTemplate >
<DataTemplate>
<TextBlock Text="{Binding Path=whatDoIPutHere, Converter={BLL:CodeMarkupExtension}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In my code behind I do this
AgencyTypeListBox.ItemsSource = (List<int>) someListofInts;
If the ItemsSource is a list of ints, the data context of each item will already be an integer list entry; you don't need to specify any path.
<TextBlock Text="{Binding Converter={BLL:CodeMarkupExtension}}" />