在 WPF 控件上实现 SelectedValues 属性

发布于 2024-07-23 07:23:16 字数 1620 浏览 4 评论 0原文

我正在创建一个 CheckedComboBox WPF 控件。 我想添加一个可以通过 XAML 绑定的 SelectedValuesProperty。 我尝试了一些不同的方法,但还无法让它发挥作用。 有人对如何解决这个问题有什么建议吗? 我的控件继承自MultiSelector。 提前致谢!

这是我到目前为止所拥有的,问题是我无法从对象中获取 itemcontainer:

public static readonly DependencyProperty SelectedValuesProperty = DependencyProperty.Register( 
  "SelectedValues", typeof( IEnumerable ), typeof( CheckedComboBox ),
      new FrameworkPropertyMetadata( ( IEnumerable ) null,
        new PropertyChangedCallback( OnSelectedValuesChanged ) ) );

private static void OnSelectedValuesChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
  CheckedComboBox combo = ( CheckedComboBox ) d;
  IEnumerable oldValue = ( IEnumerable ) e.OldValue;
  IEnumerable newValue = ( IEnumerable ) e.NewValue;

  // unselect all the old vlaues
  if ( oldValue != null )
  {
    foreach ( object obj in oldValue )
    {
      CheckedComboBoxItem item = obj as CheckedComboBoxItem;
      if ( item == null )
        item = combo.ItemContainerGenerator.ContainerFromItem( obj ) as CheckedComboBoxItem;
      if ( item != null && item.IsEnabled && item.IsSelected )
        item.IsSelected = false;
    }
  }

  // select all the new values
  if ( e.NewValue != null )
  {
    foreach ( object obj in newValue )
    {
      CheckedComboBoxItem item = obj as CheckedComboBoxItem;
      if ( item == null )
        item = combo.ItemContainerGenerator.ContainerFromItem( obj ) as CheckedComboBoxItem;
      if ( item != null && item.IsEnabled && !item.IsSelected )
        item.IsSelected = true;
    }
  }
}

I'm working on creating a CheckedComboBox WPF control. I want to add a SelectedValuesProperty that I could bind through via XAML. I've tried a few different things and haven't been able to get it to work yet. Does anyone have any suggestions on how to approach this? My control inherits from MultiSelector. Thanks in advance!

This is what I have so far, problem is I can't get the itemcontainer from the object:

public static readonly DependencyProperty SelectedValuesProperty = DependencyProperty.Register( 
  "SelectedValues", typeof( IEnumerable ), typeof( CheckedComboBox ),
      new FrameworkPropertyMetadata( ( IEnumerable ) null,
        new PropertyChangedCallback( OnSelectedValuesChanged ) ) );

private static void OnSelectedValuesChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
  CheckedComboBox combo = ( CheckedComboBox ) d;
  IEnumerable oldValue = ( IEnumerable ) e.OldValue;
  IEnumerable newValue = ( IEnumerable ) e.NewValue;

  // unselect all the old vlaues
  if ( oldValue != null )
  {
    foreach ( object obj in oldValue )
    {
      CheckedComboBoxItem item = obj as CheckedComboBoxItem;
      if ( item == null )
        item = combo.ItemContainerGenerator.ContainerFromItem( obj ) as CheckedComboBoxItem;
      if ( item != null && item.IsEnabled && item.IsSelected )
        item.IsSelected = false;
    }
  }

  // select all the new values
  if ( e.NewValue != null )
  {
    foreach ( object obj in newValue )
    {
      CheckedComboBoxItem item = obj as CheckedComboBoxItem;
      if ( item == null )
        item = combo.ItemContainerGenerator.ContainerFromItem( obj ) as CheckedComboBoxItem;
      if ( item != null && item.IsEnabled && !item.IsSelected )
        item.IsSelected = true;
    }
  }
}

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

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

发布评论

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

评论(1

蘸点软妹酱 2024-07-30 07:23:16

我正在尝试解决同样的问题。 我需要一个 SelectedValues (不是 SelectedItems)与 SelectedValuePath 结合使用,这样如果我传入对象集合,我可以在这些对象上指定一个属性以使用返回的值。 SelectedValues 将返回值的集合。

I'm trying to figure out this same problem. I need a SelectedValues (not SelectedItems) to use in conjunction with the SelectedValuePath so that if I pass in a Collection of objects, I can specify a property on those objects to use a the values returned. SelectedValues would return a collection of the values.

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