<TextBlock Text="{Binding PropertyName}" />如何使用
时间: 2023-04-01 08:04:15 浏览: 93
您好,感谢您的提问。关于<TextBlock Text="{Binding PropertyName}" />的使用,这是一个 WPF 控件中的数据绑定语法,用于将控件的 Text 属性与 ViewModel 中的 PropertyName 属性绑定。具体使用方法可以参考 WPF 数据绑定的相关文档和教程。
相关问题
<ItemsControl x:Class="HDSProject.Control.CustomFont" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HDSProject.Control" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" x:Name="itemCtrl" ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource Mode=Self}}" FontSize="20" Foreground="Teal" > <ItemsControl.ItemContainerStyle> <Style TargetType="ContentPresenter"> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </ItemsControl.ItemContainerStyle> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel> <Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}"> <TextBlock Text="{Binding}" /> </Border> <Rectangle Height="1" Fill="{Binding Foreground, ElementName=itemCtrl}" Margin="0,0,0,5"> <Rectangle.Style> <Style TargetType="Rectangle"> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True"> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style> </Rectangle.Style> </Rectangle> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>给这个自定义控件添加Thumb
要给这个自定义控件添加Thumb,可以在ItemsControl的外层包裹一个Grid,然后在Grid的右下角添加一个Thumb。以下是修改后的代码示例:
```
<Grid>
<ItemsControl x:Name="itemCtrl" ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource Mode=Self}}" FontSize="20" Foreground="Teal" >
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}">
<TextBlock Text="{Binding}" />
</Border>
<Rectangle Height="1" Fill="{Binding Foreground, ElementName=itemCtrl}" Margin="0,0,0,5">
<Rectangle.Style>
<Style TargetType="Rectangle">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Thumb DragDelta="Thumb_DragDelta" Width="10" Height="10" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
</Grid>
```
注意,这里我添加了一个Thumb,并且为它绑定了DragDelta事件。您需要在代码中实现Thumb_DragDelta方法,以便在Thumb被拖动时进行相应的处理。
<ItemsControl x:Class="HDSProject.Control.CustomFont" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HDSProject.Control" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" x:Name="itemCtrl" ItemsSource="{Binding Path=Title2, RelativeSource={RelativeSource Mode=Self}}" FontSize="20" Foreground="Teal" > <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border Margin="{Binding Path=CharacterSpacing, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}"> <TextBlock Text="{Binding}" /> <Border.Style> <Style TargetType="Border"> <Setter Property="BorderThickness" Value="0"/> <Style.Triggers> <DataTrigger Binding="{Binding ShowUnderline, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:CustomFont}}" Value="True"> <Setter Property="BorderThickness" Value="0 0 0 1"/> </DataTrigger> </Style.Triggers> </Style> </Border.Style> <Border.BorderBrush> <SolidColorBrush Color="{Binding Foreground.Color, ElementName=itemCtrl}" /> </Border.BorderBrush> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>Title2输入
时,可以展示多行
这段代码展示了一个自定义字体控件CustomFont,其中使用了ItemsControl来展示一个字符串数组Title2中的内容。在ItemsControl中,设置了ItemsSource属性为Title2,这样ItemsControl就会自动将Title2中的每个字符串作为一个项进行展示。同时,通过设置ItemsPanelTemplate为一个StackPanel,使得ItemsControl中的项可以横向排列。
在ItemTemplate中,使用了一个Border来包含TextBlock,用于设置每个项的边框和间距。在TextBlock中,绑定了Text属性为当前项的值。另外,通过设置Border的Style来设置边框的样式,当ShowUnderline属性为True时,会在底部添加一个下划线。
至于你提到的Title2输入时可以展示多行,我不太理解你的意思,请提供更多细节信息。
阅读全文