I have a class named Order with properties of ID and Name.
I have an ObservableCollection<Order> named AllOrder.
I have a ComboBox
<ComboBox Name="CID" DataContext="{Binding ElementName=MainScreen}"
ItemsSource="{Binding AllOrder}" DisplayMemberPath="ID"/>
When I select the ComboBox I want TextBox to display Name, something like:
<TextBox Text="{Binding ElementName=CID,Path=SelectedItem,Display=Name}">
You can bind to nested properties:
<TextBox Text="{Binding ElementName=CID, Path=SelectedItem.Name}" />
SelectedItem returns the currently selected Order object and .Name returns the value of the Name property of this one.
Related
XAML
<TextBox Text="{Binding SelectedKey.Name}" />
I want to bind an instance of SelectedKey to a TextBox, but show the property SelectedKey.Name as text.
How is this possible? I could not find a TextBox property like "DataBoundItem".
If the DataContext of the TextBlock has a property called "SelectedItem" that returns an object with a SelectedKey property you could bind to the Name property of this latter one like this:
<TextBox Text="{Binding SelectedItem.SelectedKey.Name}" />
If you want to bind to the SelectedKey property of an item that is currently selected in an ItemsControl such as for example a ListBox, you could use an ElementName binding:
<ListBox x:Name="listBox1" ... />
<TextBox Text="{Binding SelectedItem.SelectedKey.Name, ElementName=listBox1}" />
If you need any further help you should read this:
How to create a Minimal, Complete, and Verifiable example
I could not find a TextBox property like "DataBoundItem".
A TextBox doesn't have any "DataBoundItem" property. It has a Text property for displaying the text in the TextBox and that's it basically.
All controls that inherit from FrameworkElement has a Tag property of type object that you can bind anything you want to though. You can use this one:
<TextBox Text="{Binding SelectedKey.Name}" Tag="{Binding SelectedKey}" />
I have a list of notifications:
public List<Notification> Notifications { get; set; }
A ListBox in my UI binds to this list:
<ListBox ItemsSource="{Binding Notifications}" DisplayMemberPath="ServiceAddress" Name="NotificationsList"/>
In my UI, I also have this TextBox:
<TextBox Name="MatchWindowTextBox"/>
MatchWindow is a property in Notification objects...so I can access it like this from the above list: Notifications[SomeIndex].MatchWindow. Anyways, when someone changes the selection on the ListBox, this effectively selects a different Notification...so is there some way to bind my TextBox to the selected notification's MatchWindow property?
one quick way is to use an ElementName binding:
<TextBox Text="{Binding SelectedItem.MatchWindow,
ElementName=NotificationsList}"/>
However, a better approach would be to create a SelectedItem property of type Notification in the ViewModel and bind to that instead:
<ListBox ItemsSource="{Binding Notifications}"
SelectedItem="{Binding SelectedNotification}"/>
<!-- ... -->
<TextBox Text="{Binding SelectedNotification.MatchWindow}"/>
I know Autocompleteboxes generally are for binding items and selected items.
But what If I enter text in it and the item doesn't exist. And I want to bind it to a string variable is that possible?
For example
<telerik:RadAutoCompleteBox Text="{Binding Value}"
MVVM side
public string Value {get; set;}
Use SearchText property instead, e.g.:
<telerik:RadAutoCompleteBox
x:Name="acb"
SearchText="{Binding SearchText, Mode=TwoWay}"
ItemsSource="{Binding Items}" />
Don't forget to implement INotifyPropertyChanged interface to sync changes between view and viewmodel.
I'm using silverlight framework 4: I'm trying to list my items in a generic list to a listbox control: But the only data a receive is the classname itself.
lsBox => the listbox control
lsTags => generic type
My question is: how can I add my items in the generic list, to the listbox control?
my code is:
lsBox.ItemsSource = lsTags;
You can use DisplayMemberPath and SelectedValuePath properties of your ListBox control to tell ListBox which property's value should be displayed for every item and which property should be used for determening ListBox.SelectedValue property. Or use ListBox.ItemTemplate to display a complex data like this:
<ListBox x:Name="usersInGroupLBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsActive, Mode=TwoWay}" />
<TextBlock Text="{Binding User.UserName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Do not forget, you can use only public properties for binding. Check you class Tag.
The default behavior of ListBox (and most other controls) for displaying user types is to call the ToString() method. The default behavior of that is to display the class name.
What you should do depends on what you want to display, but if it's something simple like displaying the value of the Name property, just set the DisplayMemberPath property:
<ListBox Name="lsBox" DisplayMemberPath="Name" />
I have a listbox in a Silverlight C# app which is binded to some data from a database in XAML:
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding Name}" Foreground="White" FontSize="35" TextAlignment="Left"/>
<TextBlock x:Name="DetailsText" Text="{Binding Description}" Foreground="Gray" Margin="0,-6,0,3" />
</StackPanel>
Now in code behind file, i am trying to get the string "Name" (binded above) based on the selection user makes in the listbox.
I tried the SelectedIndex property of listbox but it only returns integer. Can anyone help me?
I think , ListBox's ItemSource poperty is binded to some custom collection of objects
1- if you handeling selectionchanged event than you can use SelectedItem property
that means
CustomObject obj = lstName.SelectedItem as CustomObject
if(obj!=null)
{
string name = obj.Name;
}
where lstName is name of your listbox
and CustomObject is the type of object in the ItemSource property of listbox
Try to use the SelectedItem property instead
Edit:
Or even better bind the SelectedItem property to a value in your ViewModel if you use the MVVM pattern in your application.
Have you tried using , SelectedItem property and if it is not null then try to find the TextBlock with Name as the Child of the SelectedItem using VisualTreeHelper ?