WPF小结(一&二) 布局和控件

本文是关于WPF布局和控件的小结,涵盖了XMAL命名空间、各种布局(Grid、StackPanel、DockPanel、WrapPanel)及其特性,以及控件的分类,重点讨论了WebBrowser、MediaElement、Thumb、Inkcanvas和Listbox等特殊控件的使用技巧,包括列表控件的模板定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


从个人整理小结的角度出发,现在水平有限,部分结论是错的。就xmal文件来说,结构有三,命名空间、资源、主窗口。


一、 XMAL 命名空间

二、布局和控件(窗口的主架)


1.布局:

   (1) Grid 网格布局 等比宽高度可以用来页面自适应。

   (2) StackPanel 的Orientation控制方向,HorizontalAlignment控制水平布局,VerticalAlignment控制垂直布局。

         StackPanel 留心<StackPanel><ListBox/></StackPanel> listbox 不出滚动条

   (3) DockPanel DockPanel.Dock & LastChildFill属性 very cool. 

   (4) WrapPanel 自动换行 也挺溜

2.控件

   控件分两种单个控件(如button textblock等)和列表控件(如listbox等)

   (1)觉得比较酷的控件 WebBrowser、MediaElement、Thumb、Inkcanvas & listbox

   (2)列表控件 结合ItemsPanelTemplate改写listbox container的style,Item的style 可以重写Template属性,DatatTemplate控制数据 。

 <!--ListBox WrapPanel Style-->
        <ItemsPanelTemplate x:Key="ItemsWrapPanelStyle">
            <WrapPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
        <!--ListBoxItem Score Style-->
        <Style TargetType="ListBoxItem" x:Key="ListBoxItemScoreStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="Bd" BorderThickness="0" Margin="5" Background="AliceBlue">
                            <ContentPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" TargetName="Bd" Value="Blue"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="False"/>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="Yellow"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
<ListBox ItemsPanel="{StaticResource ItemsWrapPanelStyle}" ItemContainerStyle="{StaticResource ListBoxItemScoreStyle}">
                <ListBoxItem>
                    ListBox
                </ListBoxItem>
                <ListBoxItem>
                    好棒
                </ListBoxItem>
            </ListBox>


      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值