I have a listBox whose items are to be shown in a textbox format, like so:-
<ListBox ItemsSource="{Binding movieList}" Name="innerList">
<ListBox.ItemTemplate >
<DataTemplate >
<TextBox Text="-------" TextChanged="TextBox_TextChanged_1"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
EDIT:
Sorry, movie list was an observablecollection (of Movie) instead of being (of String)
How do I get the textbox to show the contents of its ancestor (the innerList) ?
If you want to display the title of a movie in the TextBox, just use that:
<TextBox Text="{Binding Title}" TextChanged="TextBox_TextChanged_1"/>
(assuming the items in the list are objects with a Title property)
From Binding Declarations Overview
Optionally, a period (.) path can be used to bind to the current
source. For example, Text="{Binding}" is equivalent to Text="{Binding
Path=.}".
So following should do it.
<TextBox Text="{Binding}" TextChanged="TextBox_TextChanged_1"/>
Related
XAML:
<HeaderedItemsControl ItemsSource="{Binding FooCollection}">
<HeaderedItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text={Binding foo}/>
<Button Command="{Binding DeleteFoo}">Delete</Button>
</StackPanel>
</DataTemplate>
</HeaderedItemsControl.ItemTemplate>
Given this XAML, how can I get a reference to the item clicked in FooCollection, in the DeleteFoo command & method, in the ViewModel? Normally in a DataGrid etc, it would just be the SelectedItem bound in your ViewModel, but annoyingly the HeaderedItemsControl doesn't have this option.
Is there another way to pass a reference to the item, or its index position in FooCollection via CommandArguments for example?
I realise that the ListView is a similar control, that has a selectedItem equivalent, however I've got a nicely formatted HeaderedItemsControl set up (with a title header) so I'd prefer to not have to ditch that if possible.
Thanks very much in advance for any help.
I'll put it as an answer anyway.. ItemsControls set the DataContext of the ListItem to the object that they represent so you can probably get the reference to the clicked item using
<HeaderedItemsControl ItemsSource="{Binding FooCollection}">
<HeaderedItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text={Binding foo}/>
<Button Command="{Binding DeleteFoo}" CommandParameter={Binding}>Delete</Button>
</StackPanel>
</DataTemplate>
</HeaderedItemsControl.ItemTemplate>
You can send the DataContext of each item to the command via CommandParameter
<Button Command="{Binding DeleteFoo}" CommandParameter={Binding}>Delete</Button>
I have an XML file with these nodes:
<Product>
<Name>...
<Color>...
<Price>...
</Product>
I have a listbox that displays all the Name's in the XML file like this:
<ListBox Name="listBox1" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel >
<TextBlock Text = "{Binding Name}" />
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When an item in the listbox is selected, I want to display the selected product's Color and Price on 2 label controls.
How do I bind the Color and Price to the selected Name?
This means I need to get the Color and Price info from the XML file because the listbox has only the Name.
Thanks.
The SelectedItem will be the whole item, with all three elements, so something like this should do:
<StackPanel DataContext="{Binding SelectedItem, ElementName=listBox1}">
<TextBlock Text="{Binding XPath=Color}" />
<TextBlock Text="{Binding XPath=Price}" />
</StackPanel>
(Could use Binding.StringFormat to prepend a label, also the Label control itself is for labelling things, not displaying text)
I have a listbox which displays Name property from an array of Movie objects
<ListBox Name="listBox1" SelectionChanged="listBox1_SelectionChanged">
<ItemsControl ItemsSource="{Binding}" >
<ItemsControl.ItemTemplate >
<DataTemplate >
<TextBlock Name="textBlock1" Text="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ListBox>
How can I access the text of the textBlock that's inside the ListBox in Code?
I must use the value of the Name property in my code
The selected item reported by the listbox exposes you the object that owns the Name property bound in the TextBlock. At this point the game is over.
When you do the above every textblock inside the itemscontrol has a name textblock1 that too with a scope limited to each item container.
If you want each of those textblocks individually, I usually do something like:
<TextBlock Text="{Binding Name}" Loaded="TextBlock_Loaded"/>
And in the code register those textboxes in whatever way you wish. A list probably,
List<TextBlock> TextBlockList = new List<TextBlock>();
private void TextBlock_Loaded(object sender, RoutedEventArgs e)
{
TextBlockList.Add((TextBlock)sender);
}
And for example, access the stuff as:
String FirstItem = TextBlockList.ElementAt(0).Text;
I have an ItemsControl whose ItemsSource is bound to a list of ints IDs. A converter uses the IDs to look up the name that should be displayed to the user. How can I do this in XAML? Here is what I have so far, but it doesn't work:
<ItemsControl ItemsSource="{Binding Topics}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FallbackValue='topic name', Converter={StaticResource topicToStrConverter}}" Margin="0,10,0,0"/>
<Button>
<Image Source="/PlumPudding;component/Images/appbar.cancel.rest.png" />
</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Really, what I want as an argument to the converter is the entire item that is being displayed in the template - not a property of that item. What is the syntax for this?
I'm using Silverlight 4.
if Topics is List, then what you have is correct.
However, if Topics is List, and Id is a property of the Topic class, you will need to use "Path=Id". So: {Binding FallbakcValue='Bla', Path=Id, Converter={StaticResource yourConverter}
To answer you second question:
"what I want as an argument to the converter is the entire item that is being displayed in the template - not a property of that item"
This syntax you are using will pass the entire object in the List, so in your case an int is passed to the converter. Again, if it is a list, then the Topic object is passed to the converter.
Your code is right to my opinion..
It seems that problem in 'converter'. Try to debug code of topicToStrConverter.
You have to set the DataContext for the items control or for one of it's parents. If you don't do this there is no context for the binding.
Why is the first element in my combobox popup menu not shown in the selected item area of
my combobox , when I use the SelectedItem binding? Without that it is showing up ?? Using
the same code selecteditem + selectedindex that is no problem!
<ComboBox
ItemsSource="{Binding SchoolclassSubjectViewModels}"
SelectedItem="{Binding SelectedSchoolclassSubjectViewModel}"
SelectedIndex="0"
Height="23"
HorizontalAlignment="Left"
Margin="375,13,0,0"
VerticalAlignment="Top"
Width="151">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding SchoolclassName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding SubjectName}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Well as workaround I used:
SchoolclassSubjectViewModels.Add(schoolclassSubjectVM);
SelectedSchoolclassSubjectViewModel = schoolclassSubjectVM;
and this:
SelectedItem="{Binding SelectedSchoolclassSubjectViewModel,Mode=TwoWay}"
but I would prefer the xaml only way as it should really work.
It is because the reference inside your ItemsSource collection is not the same as the one in your SelectedItem property. I would venture to guess that you are using one object context to query your database for the list of SchoolclassSubject objects which the ItemsSource is bound to, but another context to query the actual data item to which you bind the SelectedItem. Even though the list contains a reference which represents the value held by your object, it is not really the same reference, but a separate instance of the same data.
There are ways to solve this issue, most of them involve using the SelectedValuePath and SelectedValue instead of the SelectedItem properties, but the concrete solution would be different depending on your particular ORM.