Question

Still learning WPF....thanks for any help.

Is there any way to Refactor this:

    <ListBox Name="lbEvents" 
                 VerticalAlignment="Stretch" 
                 SelectionMode="Multiple"
                 Loaded="lbCenterEvents_Loaded" 
                 HorizontalAlignment="Stretch"
                 BorderBrush="Transparent"
                 Background="Transparent" 
                 SelectionChanged="lbCenterEvents_SelectionChanged"
                 ItemContainerStyle="{StaticResource KioskCheckboxListItemContainer}">
            <ListBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
            </ListBox.Resources>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Grid.Column="0"
                        Margin="0,10,0,0"
                        Padding="5,30,5,10"
                        DockPanel.Dock="Top" 
                        Style="{StaticResource KioskCheckBox}"
                        Background="{StaticResource brshSecondaryColor}"
                        FontSize="26" 
                        HorizontalAlignment="Stretch"
                        IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" 
                        Content="{Binding DisplayDescriptionForKiosk}">
                    </CheckBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Into something like this:

<ListBox Name="lbEvents" Style="{StaticResource MyFinalListBox}"
                 VerticalAlignment="Stretch" 
                 SelectionMode="Multiple"
                 Loaded="lbCenterEvents_Loaded" 
                 HorizontalAlignment="Stretch"
                 BorderBrush="Transparent"
                 Background="Transparent"  />

Just trying to get an idea...I shouldn't need exact code, pseudo code should be sufficient (I hope), thanks in advance.

EDIT: I'm asking this because I'm trying to find a way to do this with the least amount of references to StaticResources. I realize I can pull out the templates and styles, but I am hoping some one can show me how reduce it to one StaticResource.

Was it helpful?

Solution

Yes you want (something like)

<UserControl>
   <UserControl.Resources>
       <DataTemplate x:Key="MyItemTemplate" DataType="{x:Type MyDataType}">
           <CheckBox Grid.Column="0"
              Margin="0,10,0,0"
              Padding="5,30,5,10"
              DockPanel.Dock="Top" 
              Style="{StaticResource KioskCheckBox}"
              Background="{StaticResource brshSecondaryColor}"
              FontSize="26" 
              HorizontalAlignment="Stretch"
              IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" 
              Content="{Binding DisplayDescriptionForKiosk}">
            </CheckBox>
        </DataTemplate>
        <Style x:Key="MyFinalListBox" TargetType="{x:Type ListBox}">
            <Setter Property="SelectionMode" Value="Multiple" />
            ... put more properties here
        </Style>
   </UserControl.Resources>
</UserControl>

<ListBox Name="lbEvents" 
         ItemTemplate="{StaticResource MyItemTemplate}"
         Style="{StaticResource MyFinalListBox}"
         VerticalAlignment="Stretch" 
         Loaded="lbCenterEvents_Loaded" 
         HorizontalAlignment="Stretch"
         BorderBrush="Transparent"
         Background="Transparent"  />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top