2

下面是我的 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
}

[结束编辑]

有人可以解决问题吗?

4

2 回答 2

2

更改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 的所有附加属性传递给未分配的 MultiBinding Converter。为了解决此问题,您必须将 DataGrid 的附加属性传递给 MultiBindingConverter。

UPDATE :

你也可以ComboBox这样使用:

<ComboBox x:Name="ComboSearchableItems"/>

并通过后面的代码将 ItemSource 分配给它,如下所示:

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

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

于 2015-01-14T09:34:42.260 回答
-1

通过使用 WPF 检查器来检查您已绑定到控件的内容。

在此处输入链接描述

于 2015-01-15T00:09:20.100 回答