如何在自定义WPF用户控件中正确使用CoerceValueCallback [英] how to propertly use CoerceValueCallback in a custom WPF usercontrol

查看:437
本文介绍了如何在自定义WPF用户控件中正确使用CoerceValueCallback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个用户控件,团队的其他成员只是将其放在视图上,然后他们可以动态创建按钮的ObservrableCollection并绑定到它(功能区栏,但高度根据我们的需求进行了定制),它可以处理放置,样式等。它工作得很好,完全适合我们的项目需求,但是它仅在初始加载时起作用,我进行了一些挖掘并意识到,这是因为我在FrameWorkMetaData中只有一个PropertyChangedCallback而我没有设置CoerceValueCallback的回调,所以我进行了一些挖掘工作并进行了设置,但我不确定到底要怎么做-我想这是一个问题,CoerceValueCallback的方法签名使它返回一个对象,并且我只想更新我的按钮每当列表更改时。忽略此返回值是否安全?也许某些代码可能会有所帮助-

I have created a user control, the other members of my team simply place it on their view and they can then dynamically create an ObservrableCollection of buttons and bind to it (a ribbon bar but highly customized to our needs), it handles placement, style etc. It works just fine and totally fits the need for our project however it only works upon initial load, I did some digging and realized that it was because I only had a PropertyChangedCallback in the FrameWorkMetaData and I didn't set up a callback for the CoerceValueCallback so I did some digging and set one up but I am not sure exactly what to do - I guess here is the question, the method signature of the CoerceValueCallback has it returning an object and I just want to update my buttons when ever the list changes. Is it safe to ignore this return value? Maybe some code might help -

这是我的PropertyChangedCallback-在他们首次分配按钮列表时被触发

Here is my PropertyChangedCallback - gets fired when they first assign the list of buttons

 public static void ButtonListUpdated(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
  int count = 0;
  var grid = (source as RibbonBarUserControl).ButtonsGrid;
  var buttons = e.NewValue as ObservableCollection<Button>;
  if (buttons != null)
  {
    grid.Children.Clear();
    foreach (RibbonButton b in buttons)
    {
      b.Margin = new Thickness(1);
      b.Style = (Style)grid.FindResource("ribbonButtonStyle");
      ColumnDefinition c = new ColumnDefinition();
      grid.ColumnDefinitions.Add(c);
      grid.Children.Add(b);
      Grid.SetRow(b, 0);
      Grid.SetColumn(b, count);
      count = ++count;
    }
  }
}

没有,这是CoerceValueCallback

No here is the CoerceValueCallback

 public static object OnButtonListModified(DependencyObject sender, Object baseValue)
{
  //What to do here? Can I essentially ignore the returned object and basically 
  //copy what I am doing in ButtonListUpdated?
  return baseValue;
}

我在网上看到了一些示例,这些示例都更简单-例如生日较少而不是结婚年生日=结婚年,当然这有点不同-我正在创建按钮并更新控件的UI。

I saw some examples online and they where much simpler - like if birthday is less than marriage year birthday = marriage year, course this is a little different - I am creating buttons and updating the UI of my control.

确定-谢谢您的建议!

OK - Thanks for any suggestions!

推荐答案

我认为您不需要使用CoerceValueCallback。相反,您应该加入 CollectionChanged 您的收藏集的事件,并在触发时更新按钮。

I don't think you need to use a CoerceValueCallback. Instead, you should hook into the CollectionChanged event of your collection and update your buttons when that is fired.

因此,例如:

public static void ButtonListUpdated(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    var control = source as RibbonBarUserControl;
    var buttons = e.OldValue as ObservableCollection<Button>;
    if (buttons != null)
        buttons.CollectionChanged -= control.OnCollectionChanged;

    buttons = e.NewValue as ObservableCollection<Button>;
    if (buttons != null)
        buttons.CollectionChanged += control.OnCollectionChanged;

    control.UpdateButtons();
}

private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    this.UpdateButtons();
}

private void UpdateButtons() {
    // TODO: Update buttons
}

您还可以使用 NotifyCollectionChangedEventArgs 的成员确定集合的更改方式,并对按钮进行最小的更改。或者您也可以每次都完全重建它们。

You could also use the members of NotifyCollectionChangedEventArgs to determine how the collection changed, and make minimal changes to buttons. Or you could just completely rebuild them everytime.

这篇关于如何在自定义WPF用户控件中正确使用CoerceValueCallback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆