ListView DataTemplate,ControlTemplate和Style

时间:2012-03-28 21:40:53

标签: wpf xaml styles templating staticresource

仍在学习WPF ....感谢您的帮助。

有没有办法重构:

    <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>

进入类似的事情:

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

只是想要一个想法...我不应该需要精确的代码,伪代码应该足够(我希望),提前谢谢。

编辑:我问这个是因为我试图用最少量的StaticResources引用来找到一种方法。我意识到我可以提取模板和样式,但我希望有人可以告诉我如何将它减少到一个StaticResource。

1 个答案:

答案 0 :(得分:0)

是的,你想要(类似的东西)

<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"  />