Can't access item in ItemsControl - silverlight

I define my ItemsControl that contain RadioButtons.
I want to access the Items contained in ItemsControl that are attached as binding child - and when I try to access the ItemsControl.Items I can't see the RadioButtons.
The code:
<ItemsControl Name="itemsControl" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Key}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

This is because you are setting the ItemTemplate of the control, ant not its items. To add items do it like this:
<ItemsControl Name="itemsControl" >
<ItemsControl.Items>
<RadioButton Content="{Binding Key}" />
</ItemsControl.Items>
</ItemsControl>

Related

How to bind multiple ViewModels into Expanders using Caliburn.Micro?

I have a XAML view that should hold instances of other views and those views should be displayed in a list, each contained in its own Expander. I'm using Caliburn.Micro and MEF to set all the components up.
The ItemsControl itself works just fine (shows the content of the view correctly):
<ItemsControl ItemsSource="{Binding CursorTools}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl cal:View.Model="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But when I set the ItemsControl's DataTemplate to be an Expander, the Caliburn no longer "finds" the view for the viewmodel (so the expander is empty):
<ItemsControl ItemsSource="{Binding CursorTools}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!--<ContentControl cal:View.Model="{Binding}" />-->
<Expander Header="{Binding Path=Title}">
<Expander.ContentTemplate>
<DataTemplate>
<ContentControl cal:View.Model="{Binding}" />
</DataTemplate>
</Expander.ContentTemplate>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The problem is that I will have many items and their content will be large enough to fill the screen, so how can I get the Expander to set its content properly?
Expander is actually a ContentControl so this should work:
<Expander Header="{Binding Path=Title}" cal:View.Model="{Binding}" />

Dynamically adding controls to View from MVVM

In WPF, I am using the MVVM model.
I have a View with a Dockpanel and I want to add dynamically StackPanels with a Label and TextBox for all Harddisks found over the Binding.
Therefore my XAML looks like:
<DockPanel Grid.Row="1" HorizontalAlignment="Stretch" Margin="5">
<ItemsControl ItemsSource="{Binding Harddisks}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel DockPanel.Dock="Right" HorizontalAlignment="Right" Margin="2.5,0,0,0">
<Label Content="{Binding Path=Label}" />
<TextBox Text="{Binding Path=GB_Free}" Width="100" IsReadOnly="True"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
It should be four Labels and TextBoxes, but only the first Label and TextBox are shown.
Why?
Your items in your ItemsControl are not actually direct children of the DockPanel. You need to change the ItemsControl to specify that the DockPanel is the Panel. The following will cause the ItemsControl to create a DockPanel and place all the items inside it (rather than the StackPanel that ItemsControl uses by default).
More Info: MSDN: ItemsControl.ItemsPanel Property
<ItemsControl ItemsSource="{Binding Harddisks}">
<ItemsControl.ItemsPanel>
<DockPanel Grid.Row="1" HorizontalAlignment="Stretch" Margin="5" />
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel DockPanel.Dock="Right" HorizontalAlignment="Right" Margin="2.5,0,0,0">
<Label Content="{Binding Path=Label}" />
<TextBox Text="{Binding Path=GB_Free}" Width="100" IsReadOnly="True"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Display list of usercontrols within itemtemplate next to other controls

I would like to display a list of usercontrols binded to a listbox
next to each usercontrol there should be a button
<ListBox ItemsSource="{Binding usercontrollist}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--usercontrol of the current binded items-->
<Button Content="x" HorizontalAlignment="Right"></Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How can i do this in xaml code
Assuming usercontrollist is a collection of UserControl, then you should be able to do this:
<ContentControl Content="{Binding}" />

How to bind AccordionItem Visibility when using Accordion.ItemTemplate in Silverlight?

Accordion item Visibility property can be bound like this:
<layoutToolkit:Accordion x:Name="MyAccordion">
<layoutToolkit:AccordionItem Visibility="{Binding IsVisible, Converter={StaticResource VisibilityConverter}}">
...
</layoutToolkit:AccordionItem>
</layoutToolkit:Accordion>
But how to bind it when using Accordion.ItemTemplate?
<layoutToolkit:Accordion ItemsSource="{Binding AcordionItems}" x:Name="MyAccordion">
<layoutToolkit:Accordion.ItemTemplate>
<DataTemplate>
...
</DataTemplate>
</layoutToolkit:Accordion.ItemTemplate>
<layoutToolkit:Accordion.ContentTemplate>
<DataTemplate>
...
</DataTemplate>
</layoutToolkit:Accordion.ContentTemplate>
</layoutToolkit:Accordion>
I can bind IsVisible to elements inside DataTemplate, but then an empty accordion item is displayed. I need to be able to show/hide the whole accordion item.
I ended up using StackPanel with multiple Accordions:
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding AcordionItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<layoutToolkit:Accordion Visibility="{Binding IsVisible, Converter=
{StaticResource VisibilityConverter}}">
<layoutToolkit:AccordionItem>
...
</layoutToolkit:AccordionItem>
</layoutToolkit:Accordion>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>

Can I use a DataTemplateSelector within a DataTemplate?

I have an ItemsControl using a StackPanel to display a list of items.
I would like a label to appear for each row, but for the content to the left of the label to be defined by a DataTemplateSelector. I do not want to redefine the label for each DataTemplate generated by the TemplateSelector.
Is this possible?
<ItemsControl ItemsSource="{Binding Path=Values}" >
<ItemsControl.Resources>
<v:MyTemplateSelector x:Key="myTemplateSelector"></v:MyTemplateSelector>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<Label>Test: </Label>
<!--What goes here should be defined by myTemplateSelector-->
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I figured it out. The solution was to use a ContentPresenter element with a ContentTemplateSelector attribute:
<DataTemplate>
<WrapPanel>
<Label>Test: </Label>
<ContentPresenter
ContentTemplateSelector="{StaticResource ResourceKey=myTemplateSelector}">
</ContentPresenter>
</WrapPanel>
</DataTemplate>

Resources