gpt4 book ai didi

c# - 附加属性不触发 IMultiValueConverter

转载 作者:太空宇宙 更新时间:2023-11-03 13:12:30 25 4
gpt4 key购买 nike

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

[结束编辑]

谁能告诉我这是什么问题?

最佳答案

改变 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 转换器。为了解决此问题,您必须将 DataGrid 的附加属性传递给 MultiBindingConverter。

更新:

您也可以像这样使用 ComboBox :

<ComboBox x:Name="ComboSearchableItems"/>

& 像这样通过代码将 ItemSource 分配给它:

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

如果您只想使用 XAML 进行绑定(bind),请引用 this link

关于c# - 附加属性不触发 IMultiValueConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27874890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com