WPF 控件专题 ContentControl 控件详解

本文介绍了WPF中的ContentControl控件,包括其定义、常用属性及使用示例。ContentControl用于显示单一内容,可通过嵌套布局控件实现复杂界面。

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

1、ContentControl 介绍

    ContentControl 表示包含一段任意类型内容的控件;也叫作内容控件。只包含一个子元素。

**************************************************************************************************************

2、常用属性介绍

    FontFamily:字体系列;    FontSize:字体大小;    FontStretch:字体在屏幕上紧缩或加宽的程度;FontWeight:字体粗细;
    
    Background:背景;    BorderBrush:边框颜色;    BorderThickness:边框宽度;    Foreground:前景色;
    
    Width/Height:宽度/高度;    Name:元素标识名称;    IsEnabled:使能,是否可用;    Margin:外边距;
    
    Opacity:透明度;    Visibility:可见性;    IsVisible:是否可见;    FlowDirection:其子元素的流动方向;
    
    LayoutTransform:在执行布局时应该应用于此元素的图形转换方式。    RenderTransform:元素的呈现位置的转换信息;
    
    RenderTransformOrigin:由RenderTransform声明的任何可能呈现转换的中心点,相对于元素的边界。
    
    HorizontalAlignment/VerticalAlignment:在父元素中组合此元素时所应用的水平对齐特征/垂直对齐特征。
    
    HorizontalContentAlignment/VerticalContentAlignment:控件内容的水平对齐方式/垂直对齐方式。

    Content:获取或设置 ContentControl 的内容。

**************************************************************************************************************

3、具体示例

<StackPanel>
	<ContentControl FontSize="20" HorizontalAlignment="Center"  VerticalAlignment="Center" Content="ContentControl" Margin="0 10" Style="{x:Null}"/>
	
	<ContentControl FontSize="20" HorizontalAlignment="Center"  VerticalAlignment="Center"  Margin="0 10">
		<CheckBox Content="ContentControl" IsChecked="True" VerticalAlignment="Center" VerticalContentAlignment="Center" Foreground="#dddddd"/>
	</ContentControl>

	<ContentControl FontSize="20" HorizontalAlignment="Center"  VerticalAlignment="Center"  Margin="0 10">
		<Image x:Name="img" Width="150" Height="100"  Source="./huahua.png" Stretch="Fill" 
			   FlowDirection="LeftToRight" Cursor="ScrollAll" Opacity="0.7"/>
	</ContentControl>

	<ContentControl FontSize="20" HorizontalAlignment="Center"  VerticalAlignment="Center"  Margin="0 10">
		<StackPanel Orientation="Horizontal">
			<TextBlock Text="ContentControl" VerticalAlignment="Center" Foreground="#dddddd"/>
			<Rectangle Margin="10 0" Width="60" Height="20" Fill="Red" VerticalAlignment="Center"/>
			<Rectangle Margin="10 0" Width="60" Height="20" Fill="Green" VerticalAlignment="Center"/>
			<Rectangle Margin="10 0" Width="60" Height="20" Fill="BurlyWood" VerticalAlignment="Center"/>
		</StackPanel>
	</ContentControl>
</StackPanel>

**************************************************************************************************************

4、效果图

 **************************************************************************************************************

5、总结和扩展

    ContentControl虽然只能包含一个子元素,但是可以通过添加布局控件进行多个控件的添加。

**************************************************************************************************************

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值