下面是我的 DataGrid,其中包含一些与弹出控件相关联的附加属性。ComboBox 由枚举填充。
<DataGrid Name="GenericDataGrid"
helpers:SearchBehaviours.SearchValue="{Binding ElementName=FindTextbox, Path=Text, UpdateSourceTrigger=PropertyChanged}"
helpers:SearchBehaviours.IsFindPopupOpen="{Binding ElementName=PopupFind, Path=IsOpen, UpdateSourceTrigger=PropertyChanged}"
helpers:SearchBehaviours.SearchableItems="{Binding ElementName=ComboSearchableItems, Path=SelectedValue, UpdateSourceTrigger=PropertyChanged}" >
</DataGrid>
<Popup x:Name="PopupFind">
<TextBox x:Name="FindTextbox" />
<ComboBox x:Name="ComboSearchableItems"
ItemsSource="{Binding Source={helpers:Enumeration {x:Type helpers:SearchItems}}}"
DisplayMemberPath="Description"
SelectedValue="{x:Static helpers:SearchItems.AllItems}"
SelectedValuePath="Value" />
</Popup>
这是处理行为的类:
class SearchBehaviours
{
// Using a DependencyProperty as the backing store for IsTextMatch. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsTextMatchProperty =
DependencyProperty.RegisterAttached("IsTextMatch", typeof(bool), typeof(SearchBehaviours), 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 static readonly DependencyProperty SearchValueProperty =
DependencyProperty.RegisterAttached("SearchValue", typeof(string), typeof(SearchBehaviours), 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 IsFindPopupOpenProperty =
DependencyProperty.RegisterAttached("IsFindPopupOpen", typeof(bool), typeof(SearchBehaviours),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits));
public static bool GetIsFindPopupOpen(DependencyObject obj)
{
return (bool)obj.GetValue(IsFindPopupOpenProperty);
}
public static void SetIsFindPopupOpen(DependencyObject obj, bool value)
{
obj.SetValue(IsFindPopupOpenProperty, value);
}
public static readonly DependencyProperty SearchableItemsProperty =
DependencyProperty.RegisterAttached("SearchableItems", typeof(SearchItems), typeof(SearchBehaviours), new PropertyMetadata(SearchItems.AllItems));
public static SearchItems GetSearchableItems(DependencyObject obj)
{
return (SearchItems)obj.GetValue(SearchableItemsProperty);
}
public static void SetSearchableItems(DependencyObject obj, SearchItems value)
{
obj.SetValue(SearchableItemsProperty, value);
}
}
问题出在以下 IMultiValueConverter
<Style TargetType="{x:Type DataGridCell}" x:Key="textCellStyle" >
<Setter Property="helpers:SearchBehaviours.IsTextMatch">
<Setter.Value>
<MultiBinding Converter="{StaticResource SearchValueConverter}" FallbackValue="False">
<Binding Path="Content.Text" RelativeSource="{RelativeSource Self}" />
<Binding Path="(helpers:SearchBehaviours.SearchValue)" RelativeSource="{RelativeSource Self}" />
<Binding Path="(helpers:SearchBehaviours.IsFindPopupOpen)" RelativeSource="{RelativeSource Self}"/>
<Binding Path="(helpers:SearchBehaviours.SearchableItems)" RelativeSource="{RelativeSource Self}"/>
<Binding />
<Binding RelativeSource="{x:Static RelativeSource.Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="helpers:SearchBehaviours.IsTextMatch" Value="True">
<Setter Property="Background" Value="DarkOrange" />
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
当弹出窗口打开和关闭时,它会触发 IMultiValueConverter。
它在文本框文本更改时触发。
但是,如果 ComboBox 中的 SelectedValue 发生更改,则不会触发。
下面是当前触发时输出非常简单的转换器。
public class SearchValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Console.WriteLine("Triggered");
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
[编辑]
public enum SearchItems
{
[Description("All Items")]
AllItems,
[Description("Selected Items")]
SelectedItems
}
[结束编辑]
有人可以解决问题吗?