Wpf 突出显示组合框项目(如果为');s文本以组合框的文本开头';s文本框
我想突出显示(加粗并更改其颜色)文本以组合框文本框文本开头的所有项目 我试着用谷歌搜索上面的问题,但我很不幸地得到了任何类似的结果,这将解决我的问题 我认为仅仅一个提示就足以解决这个问题。虽然我是个新手。如果可能的话,给我一个简单的例子 更新: 以下是我尝试的代码:Wpf 突出显示组合框项目(如果为');s文本以组合框的文本开头';s文本框,wpf,silverlight,xaml,combobox,Wpf,Silverlight,Xaml,Combobox,我想突出显示(加粗并更改其颜色)文本以组合框文本框文本开头的所有项目 我试着用谷歌搜索上面的问题,但我很不幸地得到了任何类似的结果,这将解决我的问题 我认为仅仅一个提示就足以解决这个问题。虽然我是个新手。如果可能的话,给我一个简单的例子 更新: 以下是我尝试的代码: <ComboBox x:Name="cbUnder" ItemsSource="{Binding GroupsAndCorrespondingEffects}" IsEditable="True" Se
<ComboBox x:Name="cbUnder" ItemsSource="{Binding GroupsAndCorrespondingEffects}"
IsEditable="True" SelectedItem="{Binding SelectedGroup, Mode=TwoWay}"
TextSearch.TextPath="GroupName" Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="3">
<ComboBox.ItemTemplate>
<DataTemplate>
<VirtualizingStackPanel Orientation="Horizontal">
<TextBlock Text="{Binding GroupName}" Width="250">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Text" Value="ComboBox_PART_Editable">
<Setter Property="Foreground" Value="Red"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock Text="{Binding CorrespondingEffect}" />
</VirtualizingStackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
但是我不知道我应该用什么来替换
组合框\u PART\u Editable
,我不想要整个文本,我只想使用文本。开始使用我假设您的组合框中的项仅是普通的字符串值。您必须更改该选项,并创建一个类来显示每个项目。原因是您需要一些bool
“flag”属性,您可以将其绑定到一个DataTrigger
,该属性将根据您的要求突出显示您的条目。所以你可以这样做:
public class CustomComboBoxItem : INotifyPropertyChanged
{
public string Value { get; set; } // Implement INotifyPropertyChanged correctly...
public bool IsHighlighted { get; set; } // ... here, unlike this example
}
然后,在代码隐藏或视图模型中需要集合属性:
public ObservableCollection<CustomComboBoxItem> Items { get; set; }
到目前为止,这应该看起来像一个带有文本条目的普通组合框,因此我们必须提供一个DataTemplate
,告诉条目在满足条件时突出显示。。。这就是ishighlight
属性的作用:
<DataTemplate DataType="{x:Type YourXmlNamespacePrefix:CustomComboBoxItem}">
<TextBlock Text="{Binding Value}">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsHighlighted}" Value="True">
<Setter Property="Background" Value="LightGreen" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
嗯,这比我想的要彻底一点,但你说得对。让我知道您的进展。我的组合框中的项目不是简单的字符串值。我的意思是它们是字符串,但它是一个两列组合框。请看我更新的问题。那么您已经有了CustomComboBoxItem
类的版本(显然命名为其他内容)。只需将新属性添加到该类中,并以相同的方式继续。我已尝试实现您的代码。我使用组和相应的效果
而不是项
<代码>组和相应的效果
是列表
。现在,当我尝试写下面这行时:Items[I].ishighlight=Items[I].StartsWith(inputValue)代码>我在ishighletted
和StartsWith
下得到了一条扭曲的红色线条。当我将鼠标悬停在其中一个参数上时,它会告诉我:“GroupAndCorrespondingEffect”不包含“IsHighlighted”的定义,并且找不到接受“GroupAndCorrespondingEffect”类型的第一个参数的“IsHighlighted”的扩展方法。
然后将该属性添加到类中。老实说,如果你不能解决这个错误,我开始后悔回答你的问题。。。我已经给了你一个完整的解决方案。。。你现在需要做一些工作。伙计,你认为我只是来帮你做工作的吗?我已经花了很长时间为您提供了一个解决方案,您将不得不自己适应。。。你所要做的就是遵循我给你的榜样。
<DataTemplate DataType="{x:Type YourXmlNamespacePrefix:CustomComboBoxItem}">
<TextBlock Text="{Binding Value}">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsHighlighted}" Value="True">
<Setter Property="Background" Value="LightGreen" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
public ObservableCollection<CustomComboBoxItem> Items { get; set; }
public string InputValue
{
get { return inputValue; }
set
{
inputValue = value;
NotifyPropertyChanged("Items");
for (int i = 0; i < Items.Count; i++)
{
Items[i].IsHighlighted = Items[i].StartsWith(inputValue);
}
}
}
...
<ComboBox ItemsSource="{Binding Items}" Text="{Binding InputValue}" ... />