首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >附加属性不触发IMultiValueConverter

附加属性不触发IMultiValueConverter
EN

Stack Overflow用户
提问于 2015-01-10 09:54:38
回答 2查看 677关注 0票数 2

下面是我的DataGrid,它附带了一些属性,这些属性与弹出控件相关联。ComboBox由一个枚举填充。

代码语言:javascript
复制
    <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>

下面是处理这些行为的类:

代码语言:javascript
复制
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中

代码语言:javascript
复制
<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。

当文本框文本被更改时,它将触发。

但是,如果SelectedValue在ComboBox中发生变化,则不会触发。

以下是转换器,它是相当简单的,目前输出时,触发。

代码语言:javascript
复制
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;
    }
}

编辑

代码语言:javascript
复制
public enum SearchItems
{
    [Description("All Items")]
    AllItems,
    [Description("Selected Items")]
    SelectedItems
}

结束编辑

有人能知道问题是什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-14 09:34:42

更改DataGridCell Style代码如下:

代码语言:javascript
复制
<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转换器(这不是assigned.In命令),您必须将DataGrid的附加属性传递给MultiBindingConverter。

UPDATE :

您还可以像这样使用ComboBox

代码语言:javascript
复制
<ComboBox x:Name="ComboSearchableItems"/>

&按后面的代码分配ItemSource,如下所示:

代码语言:javascript
复制
ComboSearchableItems.ItemsSource = Enum.GetValues(typeof(SearchItems));

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

票数 2
EN

Stack Overflow用户

发布于 2015-01-15 00:09:20

使用WPF检查器检查绑定到控件的内容。

在这里输入链接描述

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27874890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档