Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF UserControl exposing inner listitems
    primarykey
    data
    text
    <p>I'm trying to write a UserControl to display a list of items where each of these items is a title and a group of checkboxes. This whole will represent a form of data where the person filling it in is answering a list of questions with a 1 to 4 value. This all works and binds nicely to the window's ViewModel.</p> <p>But I've currently got the answers hardcoded in the UserControl as follows:</p> <pre><code>&lt;ListBox ItemsPanel="{StaticResource HorizontalScores}" Style="{StaticResource styleOuterListBox}" ItemContainerStyle="{StaticResource styleOuterListBoxItem}"&gt; &lt;ListBoxItem&gt;Never&lt;/ListBoxItem&gt; &lt;ListBoxItem&gt;Sometimes&lt;/ListBoxItem&gt; &lt;ListBoxItem&gt;Often&lt;/ListBoxItem&gt; &lt;ListBoxItem&gt;Always&lt;/ListBoxItem&gt; &lt;/ListBox&gt; </code></pre> <p>I would like to set these from the window's XAML or from the ViewModel as they will be different for other forms but can't see the correct incantation. How do I remove the ListBoxItems from the UserControl and use databinding instead? </p> <p>BigEdit ...</p> <p>Ok, this is the actual user control (it looks hideous but that's not the point):</p> <pre><code>&lt;UserControl x:Class="BussPerry.Scorer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:BussPerry.ViewModel" xmlns:local="clr-namespace:BussPerry"&gt; &lt;UserControl.Resources&gt; &lt;SolidColorBrush x:Key="SelectedBackgroundBrush" Color="Gray" /&gt; &lt;SolidColorBrush x:Key="SelectedForegroundBrush" Color="Red" /&gt; &lt;ItemsPanelTemplate x:Key="HorizontalScores"&gt; &lt;StackPanel Orientation="Horizontal" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;Style x:Key="styleListBox" TargetType="{x:Type ListBox}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type ListBox}"&gt; &lt;ItemsPresenter Margin="2" /&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style x:Key="styleListBoxItem" TargetType="{x:Type ListBoxItem}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt; &lt;CheckBox Name="CheckBox" Padding="1" Width="60" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"&gt; &lt;ContentPresenter HorizontalAlignment="Center"/&gt; &lt;/CheckBox&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsSelected" Value="True"&gt; &lt;Setter TargetName="CheckBox" Property="Background" Value="{StaticResource SelectedBackgroundBrush}" /&gt; &lt;Setter TargetName="CheckBox" Property="Foreground" Value="{StaticResource SelectedForegroundBrush}" /&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; &lt;ListBox ItemsPanel="{StaticResource HorizontalScores}" Style="{StaticResource styleListBox}" ItemContainerStyle="{StaticResource styleListBoxItem}" SelectedIndex="{Binding Path=Score}"&gt; &lt;ListBoxItem&gt;Never&lt;/ListBoxItem&gt; &lt;ListBoxItem&gt;Sometimes&lt;/ListBoxItem&gt; &lt;ListBoxItem&gt;Often&lt;/ListBoxItem&gt; &lt;ListBoxItem&gt;Always&lt;/ListBoxItem&gt; &lt;/ListBox&gt; &lt;/UserControl&gt; </code></pre> <p>And it's being called as follows:</p> <pre><code>&lt;ListView Name="listviewScores" ItemsSource="{Binding Path=Scores}" Margin="5" BorderThickness="0" Background="Transparent" Focusable="False" Grid.Row="3"&gt; &lt;ListView.View&gt; &lt;GridView ColumnHeaderContainerStyle="{StaticResource styleHiddenHeader}"&gt; &lt;GridView.Columns&gt; &lt;GridViewColumn&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Path=Index}" HorizontalAlignment="Right" /&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn DisplayMemberBinding="{Binding Path=Title}" /&gt; &lt;GridViewColumn &gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;local:Scorer &gt; &lt;/local:Scorer&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;/GridView.Columns&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; </code></pre> <p>What I want to do is to move the Never/Sometimes/Often/Always listboxitems from being hard coded in the user control to be databound.</p> <p>(Suggestions of "you don't want to do it like that" are also welcome!)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload