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}">
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 have a ComboBox and a couple of TextBlock fields.
I want to display the properties of a SelectedItem from the ComboBox on those Textblock's. Image
So that when I choose one of multiple user's the properties in the TextBlock will update to those of the SelectedItem. I have found an example, although it is using silverlight, and does not work entirely.
<ComboBox Grid.Row="0"
Grid.Column="0"
VerticalAlignment="Bottom"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Left"
Margin="0"
Height="40"
Name="ComboBox"
ItemsSource="{Binding UserModels}"
SelectedItem="{Binding EnteredUserModel, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}"
Style="{StaticResource ResourceKey=ComboBoxItemTextBlock}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
<TextBlock Grid.Row="1"
Grid.Column="0"
Margin="0 10 0 10" >
<Run Text="{DynamicResource firstName}" />
<Run Text=": " />
<Run Text="{Binding ElementName=ComboBox, Path=SelectedItem, UpdateSourceTrigger=PropertyChanged}" />
</TextBlock>
This is what I've tried. I added Name to the ComboBox so that I can access it's SelectedItem in my TextBlock. I need to get the SelectedItem.firstname, etc. At this stage i can only access the entire objects.
Am I missing some useful binding?
In order to show the FirstName property of the SelectedItem, just use an appropriate property path, i.e. SelectedItem.FirstName:
<Run Text="{Binding ElementName=ComboBox, Path=SelectedItem.FirstName}" />
or, since SelectedItem is bound to an EnteredUserModel property in your view model:
<Run Text="{Binding Path=EnteredUserModel.FirstName}" />
Setting UpdateSourceTrigger=PropertyChanged is not necessary. It has no effect in a OneWay Binding.
You're getting the EnteredUserModel-Object, because that's the selected item of the ComboBox. If you want the displayed text you must bind to the FirstName-Property.
Alternatively you can bind to EnteredUserModel.FirstName in your TextBox
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>
So I have a few ListViews. The first is binded to ObservaleCollection<ComPort>. All properties of ComPort may take some predefined values. Other ListViews are responsible for that properties: they show all that possible (predefined) values and SelectedItem should be the current value of that property of ComPort from the first ObservaleCollection.
I can't attach images so here is an external picture, it would make the situation clean: http://i.stack.imgur.com/ZBRRx.png
<Window.Resources>
<ResourceDictionary x:Name="rd">
<l:ComPorts x:Key="vComPorts"/>
<l:SystemPorts x:Key="vSystemPorts"/>
<l:BaudRates x:Key="vBaudRate"/>
<l:Parities x:Key="vParities"/>
<l:DataBits x:Key="vDataBits"/>
<l:StopBits x:Key="vStopBits"/>
<l:Timeouts x:Key="vTimeouts"/>
<l:ComPort x:Key="vSelectedPort"/>
</ResourceDictionary>
</Window.Resources>
...
<ListView
Name="PortsList"
Grid.Row="1"
Grid.Column="0"
Margin="5"
VerticalAlignment="Stretch"
ItemsSource="{StaticResource vComPorts}"
DataContext="{StaticResource vComPorts}"
SelectedValuePath="PortName"
SelectedValue="{Binding ElementName=SystemPortsList, Path=SelectedItem.Value}"
SelectionChanged="PortsList_SelectionChanged"
MouseDoubleClick="PortsList_MouseDoubleClick">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox />
<TextBlock Margin="5,0,0,0" Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView
x:Name="SystemPortsList"
Margin="5"
VerticalAlignment="Stretch"
DataContext="{Binding Source={StaticResource vSelectedPort}}"
ItemsSource="{Binding Source={StaticResource vSystemPortsView}}"
SelectedItem="{Binding Source={StaticResource vSelectedPort}, Path=PortName}"
MouseEnter="SystemPortsList_Refresh"
MouseLeave="SystemPortsList_Refresh"
Grid.Row="1"
Grid.Column="1" SelectionChanged="SystemPortsList_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Name="tb" Margin="5,0,0,0" Text="{Binding Path=Name}" />
</StackPanel>
</ListView.ItemTemplate>
</ListView>
I've tried to make an instance of class ComPort for saving current value of selected item from the first ListView, but anyway I can't cope with it without help. How this task should be solved?
1) Instead of handling SelectionChanged on the PortsList ListView, bind your checkbox to the ListViewItemsPanel like so:
<CheckBox IsChecked={Binding IsSelected, RelativeSource=Parent/>
2) Add an x:Name to your first ListBox, say x:Name="ComPortLB";
3) Remove DataContext on SystemPortsList;
4) Fix SelectedItem on SystemPortsList like so:
SelectedValue="{Binding ElementName=ComPortLB, Path=SelectedValue.PortName}"
I haven't tested any of this code and I haven't done this kind of stuff for a while, so I apologize for errors, but it should get you closer. I've also had to make some assumptions about your classes since you don't provide enough information.
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