所有,我创建了DataGrid
一个上面有一个搜索框的。当用户键入并且文本位于DataGrid
匹配单元格的背面颜色为橙色时。我已经做到了这一点,但现在(回顾性地)如果找不到文本(否则默认),想要将 search 更改为“红色” TextBox
。控件的 XAML 是BorderBrush
<UserControl x:Class="ResourceStudio.Views.ResourceControl"
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:viewModels="clr-namespace:ResourceStudio.ViewModels"
xmlns:dataAccess="clr-namespace:ResourceStudio.DataAccess"
xmlns:controls="clr-namespace:ResourceStudio.Controls"
mc:Ignorable="d">
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBox DockPanel.Dock="Top" Name="searchBox" BorderBrush="#FF007ACC" BorderThickness="2">
<TextBox.Resources>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="dataAccess:DataGridTextSearch.IsTextMatch" Value="False">
<Setter Property="BorderBrush" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Resources>
</TextBox>
<Grid DockPanel.Dock="Top">
<Border>
<controls:ResourceDataGrid ItemsSource="{Binding Path=Resources}"
dataAccess:DataGridTextSearch.SearchValue="{Binding ElementName=searchBox,
Path=Text, UpdateSourceTrigger=PropertyChanged}">
<controls:ResourceDataGrid.Columns>
<DataGridTextColumn Header="KeyIndex" Binding="{Binding KeyIndex}" IsReadOnly="True"/>
<DataGridTextColumn Header="FileName" Binding="{Binding FileName}" IsReadOnly="True"/>
<DataGridTextColumn Header="ResourceName" Binding="{Binding ResourceName}" IsReadOnly="False"/>
<controls:CollectionTextColumn Collection="ResourceStringList" Visibility="Collapsed"/>
</controls:ResourceDataGrid.Columns>
<controls:ResourceDataGrid.Resources>
<dataAccess:SearchValueConverter x:Key="searchValueConverter"/>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="dataAccess:DataGridTextSearch.IsTextMatch">
<Setter.Value>
<MultiBinding Converter="{StaticResource searchValueConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="Content.Text" />
<Binding RelativeSource="{RelativeSource Self}" Path="(dataAccess:DataGridTextSearch.SearchValue)" />
</MultiBinding>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="dataAccess:DataGridTextSearch.IsTextMatch" Value="True">
<Setter Property="Background" Value="Orange" />
</Trigger>
</Style.Triggers>
</Style>
</controls:ResourceDataGrid.Resources>
</controls:ResourceDataGrid>
</Border>
</Grid>
</DockPanel>
</UserControl>
我的基本Trigger
功能TextBox
什么都不做。如何TextBox
BorderBrush
使用与用于 的相同机制来更改 的颜色DataGrid
?
谢谢你的时间。
编辑。管理 theDependencyProperty
和 the的类IConverter
是
public static class DataGridTextSearch
{
public static readonly DependencyProperty SearchValueProperty =
DependencyProperty.RegisterAttached("SearchValue", typeof(string), typeof(DataGridTextSearch),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.Inherits));
public static string GetSearchValue(DependencyObject obj)
{
return (string)obj.GetValue(SearchValueProperty);
}
public static void SetSearchValue(DependencyObject obj, string value)
{
obj.SetValue(SearchValueProperty, value);
}
public static readonly DependencyProperty IsTextMatchProperty =
DependencyProperty.RegisterAttached("IsTextMatch", typeof(bool),
typeof(DataGridTextSearch), new UIPropertyMetadata(false));
public static bool GetIsTextMatch(DependencyObject obj)
{
return (bool)obj.GetValue(IsTextMatchProperty);
}
public static void SetIsTextMatch(DependencyObject obj, bool value)
{
obj.SetValue(IsTextMatchProperty, value);
}
}
public class SearchValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string cellText = values[0] == null ? String.Empty : values[0].ToString();
string searchText = values[1] as String;
if (!string.IsNullOrEmpty(searchText) &&
!string.IsNullOrEmpty(cellText))
{
return cellText.ToLower().StartsWith(searchText.ToLower());
}
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
}