WPF DataTemplate Textblock binding - wpf

I have a listbox of Students and the datatemplate for list item.
The DataTemplate has a text block named tb.
I want to set this textblock to be binded to Name property.
How can I do it in xaml form outside? (Not from the datatemplate)
<ListBox ItemsSource="{Binding l}" ItemTemplate="{Binding DataTemplate_L}" Margin="12,70,0,0">
</ListBox>
Thank you

If I understand you correctly, you're asking how you set the databinding for the textblock, that is currently in your DataTemplate? You can't set that databinding at the ListBox level; it has to be done in your DataTemplate.
In this case the DataTemplate will inherit the DataContext of each item in the list.
<DataTemplate x:Key="myDataTemplate">
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=AnotherListItemProperty}" />
</StackPanel>
</DataTemplate>
In other words - this DataTemplate is a template for each item in the list - and the DataContext will be each item in the list.

Related

ListBox Bound to SortedSet. How To Bind TextBox?

The ItemsSource (SsString) is a SortedSet string
I want to use a TextBox (not the default TextBlock) in a ListBox but I cannot figure out how to bind to the value in the SortSet.
I have tried binding with no path, Key, key, Value, and value.
<ListBox ItemsSource="{Binding Path=SsString}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Key}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The following works but it displays TextBlock.
<ListBox ItemsSource="{Binding Path=SsString}" />
You won't be able to edit the bound values. You need a class as the item so you can target a property with the Binding.Path.
(You should be able to bind this using {Binding .}, but it's one-way)

specifying binding source to usercontrol in listbox

How can I specify datasource for a user control:
<ScrollViewer VerticalScrollBarVisibility="Visible" VerticalAlignment="Stretch">
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<local:ucMyControl />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
How ucMyControl know what values to take? I realize that lisbox will create as many ucMyControls as there are items in bondong collection?
Also, scrollviewers don't work - I can't get to 3rd control (they are all empty anyway, but I hope you'll give me leads how to bind it).
The control that is instantiated from an ItemTemplate in an ItemsControls (e.g. ListBox) has its DataContext property automatically set to the appropriate item from the Items or ItemsSource collection of the ItemsControl.
So if you for example have a collection of Person objects with properties FirstName and LastName as ListBox items you can bind like this in your UserControl:
<UserControl ...>
<StackPanel>
<TextBlock Text="{Binding Path=FirstName}"/>
<TextBlock Text="{Binding Path=LastName}"/>
</StackPanel>
</UserControl>
You do not need to set the DataContext property explicitly.

binding a Listbox in Silverlight RIA

How to bind a Listbox in silverlight with DataContext option.I want to have a resource defined in the Usercontrol and want to use it as a static resource List box control.
You bind the ItemsSource:
<ListBox ItemsSource="{Binding ElementName=MyDataSource, Path=Data }">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MyDescription}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and define a DataSource called "MyDataSource":
<riaControls:DomainDataSource x:Name="MyDataSource"...
</riaControls:DomainDataSource>

Binding a WPF ComboBox to a different ItemsSource within a ListBox DataTemplate

I have a ListBox that contains a textbox and a combobox in its datatemplate:
<ListBox Height="147" Margin="158,29,170,0" Name="PitcherListBox" VerticalAlignment="Top" ItemsSource="{Binding SomeCollectionOfObjects}" Background="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=Name}" />
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to bind the listbox to a collection of objects (which I've done successfully), but I want the combobox in the above datatemplate to have its itemssource set to a local property on the window (array of ints). I still want the combobox to have a two-way bind between its selected item and a property on the collection of objects...
I have the following in code:
PitcherListBox.DataContext = this;
Basically in the end, I want the combobox within the listbox to have a different itemssource than the listbox itself. I can't seem to figure out how to change the ComboBox's ItemsSource in XAML. Can someone provide me some feedback? Thanks!
Try this:
<ComboBox ItemsSource="{Binding LocalArrayOfIntsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourWindowTypeHere}}}" />
Note that you need to replace YourWindowTypeHere with the type of the Window containing the LocalArrayOfIntsProperty! Also remember that you will need to define an xml namespace for that type!

Silverlight: How to dynamically bind a ComboBox in a ListBox ItemTemplate?

I have a list box that requires at least one ComboBox. I couldn't find a way to place the ComboBox in the ItemTemplate I use.
...
<DataTemplate x:Key="parts_template">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<ComboBox .../>
</StackPanel>
</DataTemplate>
...
<ListBox x:Name="lb_parts" ItemTemplate="{StaticResource parts_template}" .../>
...
How do bind that ComoBox in the DataTemplate to an ObservableCollection in the code behind?
Another thing you could try is subscribe the Loaded event on the ComboBox.
Then you can set the ComboBox.ItemsSource in the EventHandler to MyObservableCollection.
Have a look
XAML:
<DataTemplate x:Key="parts_template">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<ComboBox Loaded="ComboBox_OnLoaded">
<!-- ComboBox ItemTemplate -->
</ComboBox>
</StackPanel>
</DataTemplate>
C# Code Behind:
private void ComboBox_OnLoaded(object sender, EventArgs e)
{
((ComboBox)sender).ItemsSource = MyObservableCollection;
}
Okay, here is how you can add a ComboBox to the ListBox in the code behind.
Create a ComboBox
ComboBox x = new ComboBox();
If you have a data source that populates the ComboBox then you can just bind that
x.ItemsSource = e.Result;
If you do not and want to manually add items to the ComboBox:
ComboBoxItem y = new ComboBoxItem();
Set the Content of the Item to what you want displayed in the ComboBox
y.Content = "Hello";
Now all that is left, is to add the ComboBoxItem to the ComboBox (only if you are creating the Items manually), and then the ComboBox to the ListBox
x.Items.Add(y);
//testing is my ListBox
testing.Items.Add(x);
You should be able to set the data context to the List itself
lb_Parts.DataContext=myCollection;
Then you should be able to bind to it in the template
<DataTemplate x:Key="parts_template">
<StackPanel Orientation="Horizontal">
<TextBlock .../>
<ComboBox ItemSource={Binding}/>
</StackPanel>
</DataTemplate>

Resources