對于文本框中的占位符/水印,我使用此樣式使其作業:
<Style x:Key="placeHolder" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid Margin="15 0 15 0">
<TextBox Text="{Binding Path=Text,
RelativeSource={RelativeSource TemplatedParent},
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
x:Name="textSource" TextWrapping="Wrap"
BorderBrush="Red" AcceptsReturn="True"
BorderThickness="0"
Background="Transparent"
Panel.ZIndex="2" />
<TextBox Text="{TemplateBinding Tag}" TextWrapping="Wrap" Background="{TemplateBinding Background}" Panel.ZIndex="1">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="" />
<Condition Binding="{Binding printcontrol}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="LightGray"/>
<Setter Property="Visibility" Value="Hidden"/>
</MultiDataTrigger>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="LightGray"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但是現在我需要使用 RichTextBox 并且現在這不起作用所以我嘗試過的是這樣的:
<RichTextBox x:Name="Placeholder" Height="20" IsHitTestVisible="True" VerticalAlignment="Top" Margin="0,20,298.8,0" Foreground="DarkGray" HorizontalAlignment="Right" Width="214">
<FlowDocument>
<Paragraph>
<Run Foreground="Black">
<Run.Style>
<Style TargetType="{x:Type Run}">
<Setter Property="Foreground" Value="Transparent"/>
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="" />
<Condition Binding="{Binding printcontrol}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="Foreground" Value="LightGray"/>
<Setter Property="FontSize" Value="0.004"/>
</MultiDataTrigger>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}" Value="">
<Setter Property="Foreground" Value="LightGray"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Run.Style>
</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
但這沒有用。有人知道如何獲得占位符RichTextBox嗎?
最終結果應該是這樣的:

uj5u.com熱心網友回復:
定義將確定何時顯示或隱藏占位符的依賴屬性(布爾型別)。這里唯一的問題是如何確定是否FlowDocument為空?因為RichTextBox沒有回傳檔案狀態的方法。
我認為以下帖子中描述的一個很好的解決方案(關于性能)之一:Detect if a RichTextBox is empty。
下面的代碼包含OnDocumentChanged()使用上述帖子中的解決方案的方法:
public partial class MainWindow : Window
{
public bool IsDocumentEmpty
{
get { return (bool)GetValue(IsDocumentEmptyProperty); }
set { SetValue(IsDocumentEmptyProperty, value); }
}
public static readonly DependencyProperty IsDocumentEmptyProperty =
DependencyProperty.Register("IsDocumentEmpty", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));
public MainWindow()
{
InitializeComponent();
}
private void OnDocumentChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
if (sender is RichTextBox rtb)
{
int size = rtb.Document.ContentStart.GetOffsetToPosition(rtb.Document.ContentEnd);
IsDocumentEmpty = (size == 0 // The document has not blocks. Example, after Ctrl A & Delete.
|| size == 2 // The document has 1 block without inlines. For example, 1 paragraph without inline(s).
|| size == 4); // The document has 1 block and 1 paragraph with empty inline.
}
}
}
XAML:_
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
Height="350" Width="400">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=IsDocumentEmpty}"
Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid>
<RichTextBox x:Name="rtb" IsHitTestVisible="True" HorizontalAlignment="Right"
TextChanged="OnDocumentChanged"
Background="Transparent" Panel.ZIndex="2">
<FlowDocument x:Name="fdoc"/>
</RichTextBox>
<TextBox Text=" Enter text..." TextWrapping="Wrap" Panel.ZIndex="1" Foreground="DarkGray" />
</Grid>
</Grid>
</Window>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494737.html
上一篇:從陣列創建WPFTreeView
