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.
Related
I want to do a binding to a collection of homemade custom controls.I've a collection of objects (called Parameters), each one of these should be presented in a panel by a custom control.
I have the following in my main window:
<ItemsControl Grid.Column="1"
ItemsSource="{Binding Path=Parameters}">
</ItemsControl>
And I have a resource file to declare how to view each Parameter object:
<DataTemplate DataType="{x:Type path:Parameter}">
<Grid>
<myControls:MyUserControl
Parameter="{Binding RelativeSource={RelativeSource Self}}">
</myControls:MyUserUserControl>
</Grid>
</DataTemplate>
My control takes a Parameter so I want to bind it to itself for every item in the collection. How can I do that binding?
In a DataTemplate that serves as ItemTemplate in an ItemsControl, the DataContext holds the individual items of the source collection. Hence the binding should look like this:
<myControls:MyUserControl Parameter="{Binding}" />
and the ItemsControl may be written like this to explicitly use the DataTemplate:
<ItemsControl ItemsSource="{Binding Path=Parameters}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<myControls:MyUserControl Parameter="{Binding}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
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.
I'm just starting with wpf/vmmv. I've seen examples of binding collections to list boxes. Example: in xaml , in code-behind (e.g. Page) "DataContext = collection.. ".
My view model has more properties than just a single collection that need to be bound to a view. Therefore I'd like to set the view model as DataContext for the view and then, in xaml, bind the view model's collection to a ListBox. Assuming that my view model is set as DataContext and it has a property called 'Customers', what is the correct way of binding the property to a ListBox in xaml?
I tried but it does not work.
Thanks.
Do you mean 'how do you bind a collection to a 'ListBox'? You would do that like this:
<ListBox ItemsSource="{Binding Customers}" />
Or this:
<ListBox ItemsSource="{Binding Path=Customers}" />
If you want to bind the internal values of each instance of the Customer class, you would do something like this:
<ListBox ItemsSource="{Binding Customers}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Age}" />
<TextBlock Text="{Binding EyeColour}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I guess you want to display the property "Customers", what you have to do is define ItemTemplate of ListBox, define DataTemplate inside ItemTemplate, and binding Customers to a control, just like below:
<ListBox ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Customers}"/>
......something else you want display
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<UserControl.Resources>
<DataTemplate x:Key="LstBoxTemplate">
<TextBlock Text="{Binding Item}" TextWrapping="Wrap" HorizontalAlignment="Left"/>
<Image Grid.Column="2" Margin="0,0,10,0" Visibility="{Binding isVisible,Converter={StaticResource ImageCtlVisibilityConverter}}" Source="/pjct;component/Images/im.png"/>
</DataTemplate>
</UserControl.Resources>
<ListBox x:Name=lstbox ItemsSource="{Binding itemList}" ItemTemplate="{StaticResource LstBoxTemplate}" />
You question needs more detail, and so I may be missing the point...
If your listbox is bound to a collection of custom objects that you control (aka a view model). Then it should be fairly straight forward for you to add a property to the view model that contains the object that you want to bind your listboxitem to...
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!