附加属性未触发 IMultiValueConverter



下面是我的DataGrid,其中包含一些与下面的弹出控件关联的附加属性。组合框由枚举填充。

    <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。
当文本框文本更改时,它会触发。
但是,如果组合框中的选定值发生更改,则不会触发。

下面是转换器,目前触发时输出非常简单。

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
}

[结束编辑]

有人可以是什么问题吗?

更改DataGridCell Style代码如下:

<Style TargetType="{x:Type DataGridCell}">
        <Setter Property="local:SearchBehaviours.IsTextMatch">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource SearchValueConverter}" FallbackValue="False">
                    <Binding Path="Content.Text" RelativeSource="{RelativeSource Self}" />
                    <Binding Path="(local:SearchBehaviours.SearchValue)" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}" />
                    <Binding Path="(local:SearchBehaviours.IsFindPopupOpen)" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}"/>
                    <Binding Path="(local:SearchBehaviours.SearchableItems)" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGrid}}"/>
                    <Binding />
                    <Binding RelativeSource="{x:Static RelativeSource.Self}"/>
                </MultiBinding>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="local:SearchBehaviours.IsTextMatch" Value="True">
                <Setter Property="Background" Value="DarkOrange" />
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
    </Style>

您正在将 DataGridCell 的所有附加属性传递给未分配的多绑定转换器。为了解决此问题,您必须将 DataGrid 的附加属性传递给 MultiBindingConverter。

UPDATE :

你也可以使用这样的ComboBox

<ComboBox x:Name="ComboSearchableItems"/>

并通过背后的代码分配 ItemSource,如下所示:

ComboSearchableItems.ItemsSource = Enum.GetValues(typeof(SearchItems));

如果您只想使用 XAML 进行绑定,请参阅此链接

通过使用 WPF 检查器检查绑定到控件的内容来设置星标。

在此处输入链接说明

相关内容

  • 没有找到相关文章

最新更新