交互触发器和通用属性?

发布于 2024-12-01 10:11:20 字数 694 浏览 0 评论 0原文

我创建了一个 TriggerBase 类,名称为 CollectionContainsValueTrigger。顾名思义,只要触发器包含特定值,就会调用该操作。

但是,我想创建触发器,以便它以接受 T 的各种 ObservableCollection 的方式运行,而不仅仅是显式类型的 ObservableCollection。我尝试了对象的 ObservableCollection,但绑定不起作用,因为类型与我的 ViewModel 的显式类型 ObservableCollection 不同。

我该怎么做?

xaml 示例:

  <i:Interaction.Triggers>
    <mi:CollectionContainsValueTrigger Collection="{Binding SomeStronglyTypedViewModelCollection}" Value="Some Value">
      <SomeAction />
    </mi:CollectionContainsValueTrigger>
  </i:Interaction.Triggers>

I've created a TriggerBase class, name CollectionContainsValueTrigger. As its name suggests, the trigger invokes the action whenever it contains a certain value.

However, I would like to create the trigger so it acts in such a way that it accepts all kinds of ObservableCollection of T, not just an explicit typed of ObservableCollection. I tried ObservableCollection of object but the binding doesn't work because the Type is different from my ViewModel's explicitly typed ObservableCollection.

How can I do this?

xaml example:

  <i:Interaction.Triggers>
    <mi:CollectionContainsValueTrigger Collection="{Binding SomeStronglyTypedViewModelCollection}" Value="Some Value">
      <SomeAction />
    </mi:CollectionContainsValueTrigger>
  </i:Interaction.Triggers>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

(り薆情海 2024-12-08 10:11:20

如果没有看到一些你想要实现的目标的例子,就很难提出建议。

如果我正确理解您的非泛型类需要对不同类型的某些基于泛型的类(如 ObservableCollection)进行多态访问,

为了适应这一点,我将使用多态适配器:

class ObservableCollectionAdapterBase 
{
  virtual void Method1(){}
  virtual void Method2(){}
}

class ObservableCollectionAdapter<T, TCollectionType> : ObservableCollectionAdapterBase 
where TCollectionType : IObservableCollection<T> 
{
   public ObservableCollectionAdapter(TCollectionType collection)
   {
      _collection = collection;
   }

   override void Method1(){ _collection.DoSomething(); }
   override void Method2(){ _collection.DoSomething(); }
}

It is hard to suggest without seeing some examples of what you are trying to acheive.

If i understand correctly your non-generic class needs polymorphic access to some generic-based classes (like ObservableCollection) of different types

To accomodate this I would use a polymorphic adapter:

class ObservableCollectionAdapterBase 
{
  virtual void Method1(){}
  virtual void Method2(){}
}

class ObservableCollectionAdapter<T, TCollectionType> : ObservableCollectionAdapterBase 
where TCollectionType : IObservableCollection<T> 
{
   public ObservableCollectionAdapter(TCollectionType collection)
   {
      _collection = collection;
   }

   override void Method1(){ _collection.DoSomething(); }
   override void Method2(){ _collection.DoSomething(); }
}
呆橘 2024-12-08 10:11:20

好吧,我意识到 xaml 目前不支持泛型,仅在未来版本中支持。如果是的话,那就解决了我的问题......

ok i realized that xaml does not support Generics currently, only in the future version. if it does, it would have solved my problem....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文