|
So I have this XAML code:
<Window.Resources>
<local:MainViewModel x:Key="ViewModel" />
</Window.Resources>
And this is the XAML for the actual control:
<StackPanel Orientation="Horizontal" Width="897" Margin="5" DataContext="{StaticResource ViewModel}">
<ItemsControl Width="897" ItemsSource="{Binding Intersections}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel Width="897" >
<RadioButton GroupName="Intersections"
Content="{Binding Text}"
IsChecked="{Binding IsChecked, Mode=TwoWay}" Width="150"/>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
In the code behind I have this public class:
Public Class MainViewModel
Sub New()
Intersections.Add(New Insertion() With {.Text = "Item 1"})
Intersections.Add(New Insertion() With {.Text = "Item 2", .IsChecked = True})
Intersections.Add(New Insertion() With {.Text = "Item 3"})
Intersections.Add(New Insertion() With {.Text = "Item 4"})
End Sub
Private _Intersections As New ObjectModel.ObservableCollection(Of Insertion)
Public Property Intersections() As ObjectModel.ObservableCollection(Of Insertion)
Get
Return _Intersections
End Get
Set(ByVal value As ObjectModel.ObservableCollection(Of Insertion))
_Intersections = value
End Set
End Property
Public Class Insertion
Private _Text As String
Public Property Text() As String
Get
Return _Text
End Get
Set(ByVal value As String)
_Text = value
End Set
End Property
Private _IsChecked As Boolean
Public Property IsChecked() As Boolean
Get
Return _IsChecked
End Get
Set(ByVal value As Boolean)
_IsChecked = value
End Set
End Property
End Class
End Class
The problem Im haveing is to get the RadioButtons to be shown Horizantaly, and I cant figure out how to do that. I have tried to set IsItemsHost="True" but Its giving me the error "A panel with IsItemsHost="true" is not nested in an ItemsControl. Panel must be nested in ItemsControl to get and show items." . Either I heave tried to do this in all the wrong places or Im missing some key information here.
To be clear the code works, but its giving me a stack of the RadioButtons, but I want the side by side like a WrapPanel.
The example is taken from here here[^], and I converted it to VB from C#.
modified 17-Sep-12 13:49pm.
|
|
|
|
|
Use ItemsPanelTemplate and put the radio buttons horizontally inside a stack panel.
E.g.
<ItemsControl.ItemTemplate>
<ItemsPanelTemplate>
<StackPanel Orientation='Horizontal' "/>
</ItemsPanelTemplate>
|
|
|
|
|
I tried this:
<StackPanel Orientation="Horizontal" Width="897" Margin="5" DataContext="{StaticResource ViewModel}">
<ItemsControl Width="897" ItemsSource="{Binding Intersections}">
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton GroupName="Intersections"
Content="{Binding Text}"
IsChecked="{Binding IsChecked, Mode=TwoWay}" Width="150"/>
</StackPanel>
</ItemsPanelTemplate>
</ItemsControl>
</StackPanel>
But this time it wont build properly with ItemsPanelTemplate, I have seen this on the webbefore also, but couldnt get it to work, Can you?
|
|
|
|
|
I think (though Im not sure), one needs to put the <itempresenter> tag somewhere.
|
|
|
|
|
ITs working now, but its a strange code, and its well over a page long, so I dont think I'll be pasting it here, unless you are really interested
|
|
|
|
|
Ok Found the answer here[^] . The code that works looked like this:
<StackPanel Orientation="Horizontal" Width="897" Margin="5" DataContext="{StaticResource ViewModel}">
<ItemsControl Width="897" ItemsSource="{Binding Intersections}">
<ItemsControl.Template>
<ControlTemplate>
<WrapPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
FlowDirection="LeftToRight" IsItemsHost="true">
</WrapPanel>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel Width="120">
<RadioButton GroupName="Intersections" Content="{Binding Text}" IsChecked="{Binding IsChecked, Mode=TwoWay}" Width="150"/>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
|
|
|
|