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}" />
Related
Trying for a simple thing. I want TextBlock text to be updated to what TextBox's text value is. However, I want it to happen only on LostFocus. Currently below code updates a TextBlock as user is typing into a TextBox. How do we achieve that?
<StackPanel>
<TextBox x:Name="txtQty" />
<TextBlock Text="{Binding ElementName=txtQty, Path=Text}" />
</StackPanel>
I explored the UpdateSourceTrigger property on textbox with LostFocus, but it won't work as that controls how the source should be updated, whereas here I need how the destination updates.
I prefer to have a XAML only solution.
XAML is a markup language.
The straight-forward way to to this would be to bind the TextBox and the TextBlock to the same view model source property. The source property will be set when the TextBox loses focus and then the TextBlock will then be updated provided that the view model class implements the INotifyPropertyChanged interface as expected.
You could of course also handle the LostKeyboardFocus event for the TextBox and set the Text property of the TextBlock programmatically in the code-behind of the view. This approach is not any worse than trying to implement some logic in the XAML markup of the very same view. Just because you possibly can do something in pure XAML, it doesn't mean that you always should. A programming language such as C# usually does a better job implementing some logic.
As others already said, the best way would be to bind the TextBlock and the TextBox to the same viewmodel property.
If you want to do it only with XAML code you could try it from the other side and bind your TextBox to the TextBlock.
Like this:
<StackPanel>
<TextBox Text="{Binding ElementName=txtQty, Path=Text, UpdateSourceTrigger=LostFocus, Mode=OneWayToSource}" />
<TextBlock x:Name="txtQty" />
</StackPanel>
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.
I'm trying to bind element's property in a child control to an element's property ina parent window, it doesn't work..
Here is png of what I'm trying to do:
Here is the xaml that doesn't work:
CurrentDate="{Binding ElementName=TimeBar, Path=SelectionStart,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
Thanks.
create a dependency property in your usercontrol and then bind to it in your window
something like that: DependencyProperty implementations you can find all around here on stackoverflow
<YourUsercontrol x:Name="uc">
<YourSomeControl CurrentDate="{Binding ElementName=uc, Path=MyDp}"/>
</YourUsercontrol>
xaml window
<Window>
<ElementInParent x:Name="eip" />
<YourUsercontrol MyDp="{Binding ElementName=eip, Path=PropertyFromElementInParent}"/>
based on the following Answer LINK the SelectionStart is not a Bindable Probperty by default so you need to create a attached behavior or something similar
Binding ElementName along with Relative Source is not correct approach.
Besides the UserControl does not know the ElementName of the Parent since the two are in different XAML.
One approach is to set the data context of user control with the element name you want to bind it to and then use normal binding Path.
As shown in the example below:
In main window, we have a textbox and a user control.
We are setting data context of the user control with the text box.
In the user control, we are binding the Text property of the DataContext (which is essentially TextBox of main window).
<Window
xmlns:self="clr-namespace:experiments"
>
<StackPanel>
<TextBox x:Name="Name" Width="100"/>
<self:UserControl1 DataContext="{Binding ElementName=Name}"/>
</StackPanel>
</Window>
<UserControl x:Class="experiments.UserControl1">
<Grid>
<TextBlock Text="{Binding Path=Text}" Width="100" Background="AliceBlue" Height="50"/>
</Grid>
</UserControl>
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 ?