在文本块 (WPF) 中突出显示部分文本时未知 space
Unknown space while highlighting part of text in a texblock (WPF)
通过反复试验,我设法突出显示了文本块中的部分文本,该文本块位于绑定到自定义 class 的 属性 的列表框的数据模板中。但现在的问题是,当突出显示文本时,我在突出显示的文本和其余文本之间得到一个奇怪的未知 space。
这是XAML
的一部分
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox Name="textBox1" TextChanged="textBox1_TextChanged"/>
<ListBox Grid.Row="1" Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Name="gridOfListbox" Height="25" Margin="0,2">
<DockPanel Name="dockpanelWithTxtBlock">
<TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
<Run Background="Yellow" Text=""></Run>
<Run Text="{Binding ProductID}"></Run>
</TextBlock>
</DockPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
这里使用了部分代码
ObservableCollection<TItem> items = new ObservableCollection<TItem>();
TItem[] source = new TItem[] { new TItem("Hello"), new TItem("World"), new TItem("System"), new TItem("SystemDefault"), new TItem("SystemFolder") };
并且事件的方法改变了text
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string match = textBox1.Text;
foreach (TItem TItem in listBox1.Items)
{
ListBoxItem lbi = (ListBoxItem)this.listBox1.ItemContainerGenerator.ContainerFromItem(TItem);
TextBlock txtBlck = FindFirstElementInVisualTree<TextBlock>(lbi);
Run bold = (Run)txtBlck.Inlines.FirstInline;
Run normal = (Run)txtBlck.Inlines.LastInline;
string s = bold.Text + normal.Text;
if (s.ToLower().StartsWith(match.ToLower()))
{
bold.Text = s.Substring(0, match.Length);
normal.Text = s.Substring(match.Length);
}
else
{
bold.Text = "";
normal.Text = s;
}
}
}
FindFirstElementInVisualTree 方法用于查找需要搜索的文本框。
如果需要代码,请告诉我。
我还添加了一张图片来演示问题所在。
帮助将不胜感激!
Link 图片:http://i.stack.imgur.com/rOj0m.png
当您在 XAML 中的 TextBlock
中使用 Run
时,未包含在 <>
中的所有内容都被视为实际字符串。换行意味着 space。将两个 Run
放在同一行中(中间也没有 space)。
<TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
<Run Background="Yellow" Text="" /><Run Text="{Binding ProductID}" />
</TextBlock>
编辑
顺便说一句,我刚刚看到您的 first question 被标记为重复。这个问题问对了;所以你以后应该用这种方式提问。
通过反复试验,我设法突出显示了文本块中的部分文本,该文本块位于绑定到自定义 class 的 属性 的列表框的数据模板中。但现在的问题是,当突出显示文本时,我在突出显示的文本和其余文本之间得到一个奇怪的未知 space。
这是XAML
的一部分<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox Name="textBox1" TextChanged="textBox1_TextChanged"/>
<ListBox Grid.Row="1" Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Name="gridOfListbox" Height="25" Margin="0,2">
<DockPanel Name="dockpanelWithTxtBlock">
<TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
<Run Background="Yellow" Text=""></Run>
<Run Text="{Binding ProductID}"></Run>
</TextBlock>
</DockPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
这里使用了部分代码
ObservableCollection<TItem> items = new ObservableCollection<TItem>();
TItem[] source = new TItem[] { new TItem("Hello"), new TItem("World"), new TItem("System"), new TItem("SystemDefault"), new TItem("SystemFolder") };
并且事件的方法改变了text
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string match = textBox1.Text;
foreach (TItem TItem in listBox1.Items)
{
ListBoxItem lbi = (ListBoxItem)this.listBox1.ItemContainerGenerator.ContainerFromItem(TItem);
TextBlock txtBlck = FindFirstElementInVisualTree<TextBlock>(lbi);
Run bold = (Run)txtBlck.Inlines.FirstInline;
Run normal = (Run)txtBlck.Inlines.LastInline;
string s = bold.Text + normal.Text;
if (s.ToLower().StartsWith(match.ToLower()))
{
bold.Text = s.Substring(0, match.Length);
normal.Text = s.Substring(match.Length);
}
else
{
bold.Text = "";
normal.Text = s;
}
}
}
FindFirstElementInVisualTree 方法用于查找需要搜索的文本框。
如果需要代码,请告诉我。 我还添加了一张图片来演示问题所在。 帮助将不胜感激! Link 图片:http://i.stack.imgur.com/rOj0m.png
当您在 XAML 中的 TextBlock
中使用 Run
时,未包含在 <>
中的所有内容都被视为实际字符串。换行意味着 space。将两个 Run
放在同一行中(中间也没有 space)。
<TextBlock Name="textbloxk" DockPanel.Dock="Left" FontSize="15" TextAlignment="Center">
<Run Background="Yellow" Text="" /><Run Text="{Binding ProductID}" />
</TextBlock>
编辑
顺便说一句,我刚刚看到您的 first question 被标记为重复。这个问题问对了;所以你以后应该用这种方式提问。